Reads a value from a blob
Device + Agent
Name | Type | Description |
---|---|---|
dataType | Constant |
A single-character string constant indicating the value’s data type
|
A value of the specified type
This method reads a single value of a specified type from the blob. The blob’s read/write pointer governs the first byte to be read. The value type is specified by a single-character string constant which must be delimited with single quote marks:
local floatValue = dataBlob.readn('f');
Of Squirrel’s own data types only float and integer are supported (both are 32-bit values), but readn() additionally supports a number of the most common 8-bit and 16-bit standard variable types. The following table lists the data types supported and their data type strings:
String Constant | Type |
---|---|
'c' | 8-bit signed integer |
'b' | 8-bit unsigned integer |
's' | 16-bit signed integer |
'w' | 16-bit unsigned integer |
'i' | 32-bit signed integer |
'f' | 32-bit float |
'd' | 64-bit float |
Note that reading in a 64-bit value — an IEEE 754 double-precision float — will implicitly convert the 64-bit number to a standard 32-bit Squirrel float with the inevitable loss of precision that this entails.
All imps are little endian, so multi-byte values will written into a blob or read from it in order of least byte significance. For example, if a 16-bit unsigned integer is written into a blob (the 'w' option, above), the least-significant byte is written first (at blob pointer location n) and followed by the most-significant byte (at blob pointer location n+1).
Use the following code to convert a signed integer value of n bits into a 32-bit Squirrel integer and preserve the sign. For a 16-bit value:
local readInt = blob.readn('s');
local squirrelInt = (readInt << 16) >> 16;
For an 8-bit value:
local readInt = blob.readn('c');
local squirrelInt = (readInt << 24) >> 24;