Reads a value from a blob
Device + Agent
Name | Type | Description |
---|---|---|
dataType | Constant |
A single-byte integer 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-byte integer. For convenience, you can use Squirrel’s character representation rather than numeric values, for example:
local floatValue = dataBlob.readn('f');
this is equivalent to:
local floatValue = dataBlob.readn(102);
but makes the value type much more clear. When using characters to represent integers this way, you must use single quotes as delimiters.
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:
Constant | Type |
---|---|
'c' / 99 | 8-bit signed integer |
'b' / 98 | 8-bit unsigned integer |
's' / 115 | 16-bit signed integer |
'w' / 119 | 16-bit unsigned integer |
'i' / 105 | 32-bit signed integer |
'f' / 102 | 32-bit float |
'd' / 100 | 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;