pack
Library that packs & unpacks types as strings, same as bytepack does with bytes.
Functions with a size in their name (readUInt16BE, writeFloatLE, readNullTerminatedString, ...) read from a 1-based byte index of the string, same as string.byte, and write a new string:
lua Copy
local data = pack.writeUInt16BE ( 300 ) .. pack.writeFloatLE ( 1.5 ) --> "\1\44\0\0\192\63"
pack.readUInt16BE ( data, 1 ) --> 300
pack.readFloatLE ( data, 3 ) --> 1.5 Bit functions (readBit, readBoolean, readBytes, readUInt, readInt, readFixedPoint, readUnsignedFixedPoint) read from a 0-based bit_position instead, so they also read fields that are not aligned to bytes.
Bits are stored LSB-first (bit 0 of the data is the lowest bit of the first byte), and little endian integers are stored lowest bit first, same as Source's bf_write/bf_read, so data is compatible with gmod net.Write*/net.Read*.
All reads throw EndOfFileError when the string is too short.
stack_level of local functions is the level relative to their caller, public functions that tail call them pass 1 (tail calls drop the frame), others pass 2.
Functions pack.readBitReads one bit from the string. pack.readBooleanReads one bit from the string as a boolean, same as net.ReadBool. pack.readBytesReads bytes from any bit position of the string. pack.readCountedStringReads the string prefixed with its length. pack.readDateBEReads 2-byte (16 bit) DOS date from big endian bytes of the string. pack.readDateLEReads 2-byte (16 bit) DOS date from little endian bytes of the string. pack.readDoubleBEReads double precision (8-byte, 64 bit) floating point number from big endian bytes of the string. pack.readDoubleLEReads double precision (8-byte, 64 bit) floating point number from little endian bytes of the string. pack.readFixedPointReads signed fixed-point number (m integer bits including the sign bit and n fractional bits) from any bit position of… pack.readFixedStringReads the string of a fixed size. pack.readFloatBEReads single precision (4-byte, 32 bit) floating point number from big endian bytes of the string. pack.readFloatLEReads single precision (4-byte, 32 bit) floating point number from little endian bytes of the string. pack.readIntReads signed (two's complement) integer of any size from any bit position of the string. pack.readInt16BEReads signed 2-byte (16 bit) integer from big endian bytes of the string. pack.readInt16LEReads signed 2-byte (16 bit) integer from little endian bytes of the string. pack.readInt24BEReads signed 3-byte (24 bit) integer from big endian bytes of the string. pack.readInt24LEReads signed 3-byte (24 bit) integer from little endian bytes of the string. pack.readInt32BEReads signed 4-byte (32 bit) integer from big endian bytes of the string. pack.readInt32LEReads signed 4-byte (32 bit) integer from little endian bytes of the string. pack.readInt40BEReads signed 5-byte (40 bit) integer from big endian bytes of the string. pack.readInt40LEReads signed 5-byte (40 bit) integer from little endian bytes of the string. pack.readInt48BEReads signed 6-byte (48 bit) integer from big endian bytes of the string. pack.readInt48LEReads signed 6-byte (48 bit) integer from little endian bytes of the string. pack.readInt56BEReads signed 7-byte (56 bit) integer from big endian bytes of the string. pack.readInt56LEReads signed 7-byte (56 bit) integer from little endian bytes of the string. pack.readInt64BEReads signed 8-byte (64 bit) integer from big endian bytes of the string. pack.readInt64LEReads signed 8-byte (64 bit) integer from little endian bytes of the string. pack.readInt8Reads signed 1-byte (8 bit) integer from the string. pack.readLineTerminatedStringReads the line terminated by \n (a trailing \r is removed), or the rest of the data if there is no \n. pack.readNullTerminatedStringReads C-style string, the null terminator (\0) is required. pack.readTerminatedStringReads bytes until the terminator byte, or the rest of the data if there is no terminator. pack.readTimeBEReads 2-byte (16 bit) DOS time from big endian bytes of the string. pack.readTimeLEReads 2-byte (16 bit) DOS time from little endian bytes of the string. pack.readUIntReads unsigned integer of any size from any bit position of the string. pack.readUInt16BEReads unsigned 2-byte (16 bit) integer from big endian bytes of the string. pack.readUInt16LEReads unsigned 2-byte (16 bit) integer from little endian bytes of the string. pack.readUInt24BEReads unsigned 3-byte (24 bit) integer from big endian bytes of the string. pack.readUInt24LEReads unsigned 3-byte (24 bit) integer from little endian bytes of the string. pack.readUInt32BEReads unsigned 4-byte (32 bit) integer from big endian bytes of the string. pack.readUInt32LEReads unsigned 4-byte (32 bit) integer from little endian bytes of the string. pack.readUInt40BEReads unsigned 5-byte (40 bit) integer from big endian bytes of the string. pack.readUInt40LEReads unsigned 5-byte (40 bit) integer from little endian bytes of the string. pack.readUInt48BEReads unsigned 6-byte (48 bit) integer from big endian bytes of the string. pack.readUInt48LEReads unsigned 6-byte (48 bit) integer from little endian bytes of the string. pack.readUInt56BEReads unsigned 7-byte (56 bit) integer from big endian bytes of the string. pack.readUInt56LEReads unsigned 7-byte (56 bit) integer from little endian bytes of the string. pack.readUInt64BEReads unsigned 8-byte (64 bit) integer from big endian bytes of the string. pack.readUInt64LEReads unsigned 8-byte (64 bit) integer from little endian bytes of the string. pack.readUInt8Reads unsigned 1-byte (8 bit) integer from the string. pack.readUnsignedFixedPointReads unsigned fixed-point number (m integer bits and n fractional bits) from any bit position of the string. pack.writeCountedStringWrites the string prefixed with its length. pack.writeDateBEWrites 2-byte (16 bit) DOS date as big endian bytes. pack.writeDateLEWrites 2-byte (16 bit) DOS date as little endian bytes. pack.writeDoubleBEWrites double precision (8-byte, 64 bit) floating point number as big endian bytes. pack.writeDoubleLEWrites double precision (8-byte, 64 bit) floating point number as little endian bytes. pack.writeFixedStringWrites the string of a fixed size, shorter data is padded with \0 and longer data is cut. pack.writeFloatBEWrites single precision (4-byte, 32 bit) floating point number as big endian bytes. pack.writeFloatLEWrites single precision (4-byte, 32 bit) floating point number as little endian bytes. pack.writeInt16BEWrites signed 2-byte (16 bit) integer as big endian bytes. pack.writeInt16LEWrites signed 2-byte (16 bit) integer as little endian bytes. pack.writeInt24BEWrites signed 3-byte (24 bit) integer as big endian bytes. pack.writeInt24LEWrites signed 3-byte (24 bit) integer as little endian bytes. pack.writeInt32BEWrites signed 4-byte (32 bit) integer as big endian bytes. pack.writeInt32LEWrites signed 4-byte (32 bit) integer as little endian bytes. pack.writeInt40BEWrites signed 5-byte (40 bit) integer as big endian bytes. pack.writeInt40LEWrites signed 5-byte (40 bit) integer as little endian bytes. pack.writeInt48BEWrites signed 6-byte (48 bit) integer as big endian bytes. pack.writeInt48LEWrites signed 6-byte (48 bit) integer as little endian bytes. pack.writeInt56BEWrites signed 7-byte (56 bit) integer as big endian bytes. pack.writeInt56LEWrites signed 7-byte (56 bit) integer as little endian bytes. pack.writeInt64BEWrites signed 8-byte (64 bit) integer as big endian bytes. pack.writeInt64LEWrites signed 8-byte (64 bit) integer as little endian bytes. pack.writeInt8Writes signed 1-byte (8 bit) integer as a string. pack.writeLineTerminatedStringWrites the line, the string followed by \n. pack.writeNullTerminatedStringWrites C-style string, the string followed by the null terminator (\0). pack.writeTerminatedStringWrites the string followed by the terminator byte. pack.writeTimeBEWrites 2-byte (16 bit) DOS time as big endian bytes. pack.writeTimeLEWrites 2-byte (16 bit) DOS time as little endian bytes. pack.writeUInt16BEWrites unsigned 2-byte (16 bit) integer as big endian bytes. pack.writeUInt16LEWrites unsigned 2-byte (16 bit) integer as little endian bytes. pack.writeUInt24BEWrites unsigned 3-byte (24 bit) integer as big endian bytes. pack.writeUInt24LEWrites unsigned 3-byte (24 bit) integer as little endian bytes. pack.writeUInt32BEWrites unsigned 4-byte (32 bit) integer as big endian bytes. pack.writeUInt32LEWrites unsigned 4-byte (32 bit) integer as little endian bytes. pack.writeUInt40BEWrites unsigned 5-byte (40 bit) integer as big endian bytes. pack.writeUInt40LEWrites unsigned 5-byte (40 bit) integer as little endian bytes. pack.writeUInt48BEWrites unsigned 6-byte (48 bit) integer as big endian bytes. pack.writeUInt48LEWrites unsigned 6-byte (48 bit) integer as little endian bytes. pack.writeUInt56BEWrites unsigned 7-byte (56 bit) integer as big endian bytes. pack.writeUInt56LEWrites unsigned 7-byte (56 bit) integer as little endian bytes. pack.writeUInt64BEWrites unsigned 8-byte (64 bit) integer as big endian bytes. pack.writeUInt64LEWrites unsigned 8-byte (64 bit) integer as little endian bytes. pack.writeUInt8Writes unsigned 1-byte (8 bit) integer as a string.
function pack.readBit (str : string , bit_position? : integer ): 0 | 1
Reads one bit from the string.
lua Copy
readBit ( "\5" , 2 ) --> 1
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the bit (0-based), 0 by default.
Returns
Type Name Description 0 | 1valueThe bit.
function pack.readBoolean (str : string , bit_position? : integer ): boolean
Reads one bit from the string as a boolean, same as net.ReadBool.
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the bit (0-based), 0 by default.
Returns
Type Name Description boolean valuetrue if the bit is 1.
function pack.readBytes (str : string , bit_position? : integer , byte_count : integer ): integer
Reads bytes from any bit position of the string.
lua Copy
readBytes ( "\16\32" , 4 , 1 ) --> 0x01
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the first bit (0-based), 0 by default. byte_countinteger The number of bytes to read.
Returns
Type Name Description integer ...The bytes.
function pack.readCountedString (str : string , index? : integer , length_size : integer , is_little_endian? : boolean ): string , integer
Reads the string prefixed with its length.
lua Copy
readCountedString ( "\3abc" , 1 , 1 ) --> "abc", 4
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte of the length (1-based), 1 by default. length_sizeinteger The size of the length in bytes (1 - 8). is_little_endianoptional boolean true if the length is little endian, false by default.
Returns
Type Name Description string dataThe string. integer data_lengthThe number of bytes read, including the length.
function pack.readDateBE (str : string , index? : integer ): integer , integer , integer
Reads 2-byte (16 bit) DOS date from big endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer dayThe day. integer monthThe month. integer yearThe year.
function pack.readDateLE (str : string , index? : integer ): integer , integer , integer
Reads 2-byte (16 bit) DOS date from little endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer dayThe day. integer monthThe month. integer yearThe year.
function pack.readDoubleBE (str : string , index? : integer ): number
Reads double precision (8-byte, 64 bit) floating point number from big endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description number valueThe floating point number.
function pack.readDoubleLE (str : string , index? : integer ): number
Reads double precision (8-byte, 64 bit) floating point number from little endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description number valueThe floating point number.
function pack.readFixedPoint (str : string , bit_position? : integer , m : integer , n : integer , is_little_endian? : boolean ): number
Reads signed fixed-point number (m integer bits including the sign bit and n fractional bits) from any bit position of the string.
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the first bit (0-based), 0 by default. minteger The number of integer bits, including the sign bit. ninteger The number of fractional bits. is_little_endianoptional boolean true for little endian, false by default.
Returns
Type Name Description number valueThe signed fixed-point number.
function pack.readFixedString (str : string , index? : integer , length? : integer ): string
Reads the string of a fixed size.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default. lengthoptional integer The size of the string in bytes, the rest of the data by default.
Returns
Type Name Description string dataThe string.
function pack.readFloatBE (str : string , index? : integer ): number
Reads single precision (4-byte, 32 bit) floating point number from big endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description number valueThe floating point number.
function pack.readFloatLE (str : string , index? : integer ): number
Reads single precision (4-byte, 32 bit) floating point number from little endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description number valueThe floating point number.
function pack.readInt (str : string , bit_position? : integer , bit_count : integer , is_little_endian? : boolean ): integer
Reads signed (two's complement) integer of any size from any bit position of the string.
Byte order is the same as in readUInt.
Valid values without loss of precision: -9007199254740991 - 9007199254740991 (53 bits)
lua Copy
readInt ( "\255\255" , 0 , 16 , true ) --> -1
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the first bit (0-based), 0 by default. bit_countinteger The size of the integer in bits (1 - 64). is_little_endianoptional boolean true for little endian, false by default.
Returns
Type Name Description integer valueThe signed integer.
function pack.readInt16BE (str : string , index? : integer ): integer
Reads signed 2-byte (16 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -32768 - 32767
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 2-byte integer.
function pack.readInt16LE (str : string , index? : integer ): integer
Reads signed 2-byte (16 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -32768 - 32767
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 2-byte integer.
function pack.readInt24BE (str : string , index? : integer ): integer
Reads signed 3-byte (24 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -8388608 - 8388607
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 3-byte integer.
function pack.readInt24LE (str : string , index? : integer ): integer
Reads signed 3-byte (24 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -8388608 - 8388607
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 3-byte integer.
function pack.readInt32BE (str : string , index? : integer ): integer
Reads signed 4-byte (32 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -2147483648 - 2147483647
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 4-byte integer.
function pack.readInt32LE (str : string , index? : integer ): integer
Reads signed 4-byte (32 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -2147483648 - 2147483647
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 4-byte integer.
function pack.readInt40BE (str : string , index? : integer ): integer
Reads signed 5-byte (40 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -549755813888 - 549755813887
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 5-byte integer.
function pack.readInt40LE (str : string , index? : integer ): integer
Reads signed 5-byte (40 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -549755813888 - 549755813887
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 5-byte integer.
function pack.readInt48BE (str : string , index? : integer ): integer
Reads signed 6-byte (48 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -140737488355328 - 140737488355327
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 6-byte integer.
function pack.readInt48LE (str : string , index? : integer ): integer
Reads signed 6-byte (48 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -140737488355328 - 140737488355327
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 6-byte integer.
function pack.readInt56BE (str : string , index? : integer ): integer
Reads signed 7-byte (56 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -36028797018963968 - 36028797018963967
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 7-byte integer.
function pack.readInt56LE (str : string , index? : integer ): integer
Reads signed 7-byte (56 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -36028797018963968 - 36028797018963967
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 7-byte integer.
function pack.readInt64BE (str : string , index? : integer ): integer
Reads signed 8-byte (64 bit) integer from big endian bytes of the string.
Valid values without loss of precision: -9007199254740991 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 8-byte integer.
function pack.readInt64LE (str : string , index? : integer ): integer
Reads signed 8-byte (64 bit) integer from little endian bytes of the string.
Valid values without loss of precision: -9007199254740991 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 8-byte integer.
function pack.readInt8 (str : string , index? : integer ): integer
Reads signed 1-byte (8 bit) integer from the string.
Valid values without loss of precision: -128 - 127
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe signed 1-byte integer.
# pack.readLineTerminatedString
function pack.readLineTerminatedString (str : string , index? : integer ): string , integer
Reads the line terminated by \n (a trailing \r is removed), or the rest of the data if there is no \n.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description string lineThe line without \r\n or \n. integer data_lengthThe number of bytes read, including \r\n or \n.
# pack.readNullTerminatedString
function pack.readNullTerminatedString (str : string , index? : integer ): string , integer
Reads C-style string, the null terminator (\0) is required.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description string dataThe bytes before the null terminator. integer data_lengthThe number of bytes read, including the null terminator.
# pack.readTerminatedString
function pack.readTerminatedString (str : string , index? : integer , terminator : integer ): string , integer , boolean
Reads bytes until the terminator byte, or the rest of the data if there is no terminator.
lua Copy
readTerminatedString ( "key=value" , 1 , 0x3D ) --> "key", 4, true
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default. terminatorinteger The terminator byte (0 - 255).
Returns
Type Name Description string dataThe bytes before the terminator. integer data_lengthThe number of bytes read, including the terminator if it was found. boolean terminatedtrue if the terminator was found.
function pack.readTimeBE (str : string , index? : integer ): integer , integer , integer
Reads 2-byte (16 bit) DOS time from big endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer hoursThe number of hours. integer minutesThe number of minutes. integer secondsThe number of seconds, will be rounded .
function pack.readTimeLE (str : string , index? : integer ): integer , integer , integer
Reads 2-byte (16 bit) DOS time from little endian bytes of the string.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer hoursThe number of hours. integer minutesThe number of minutes. integer secondsThe number of seconds, will be rounded .
function pack.readUInt (str : string , bit_position? : integer , bit_count : integer , is_little_endian? : boolean ): integer
Reads unsigned integer of any size from any bit position of the string.
Little endian integers are read lowest bit first, same as net.ReadUInt.
Big endian integers are read as the highest bit_count % 8 bits first, then whole bytes from the most significant to the least significant.
Valid values without loss of precision: 0 - 9007199254740991 (53 bits)
lua Copy
readUInt ( "\44\1" , 0 , 16 , true ) --> 300
readUInt ( "\5" , 1 , 2 , true ) --> 2
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the first bit (0-based), 0 by default. bit_countinteger The size of the integer in bits (1 - 64). is_little_endianoptional boolean true for little endian, false by default.
Returns
Type Name Description integer valueThe unsigned integer.
function pack.readUInt16BE (str : string , index? : integer ): integer
Reads unsigned 2-byte (16 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 65535
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 2-byte integer.
function pack.readUInt16LE (str : string , index? : integer ): integer
Reads unsigned 2-byte (16 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 65535
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 2-byte integer.
function pack.readUInt24BE (str : string , index? : integer ): integer
Reads unsigned 3-byte (24 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 16777215
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 3-byte integer.
function pack.readUInt24LE (str : string , index? : integer ): integer
Reads unsigned 3-byte (24 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 16777215
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 3-byte integer.
function pack.readUInt32BE (str : string , index? : integer ): integer
Reads unsigned 4-byte (32 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 4294967295
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 4-byte integer.
function pack.readUInt32LE (str : string , index? : integer ): integer
Reads unsigned 4-byte (32 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 4294967295
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 4-byte integer.
function pack.readUInt40BE (str : string , index? : integer ): integer
Reads unsigned 5-byte (40 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 1099511627775
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 5-byte integer.
function pack.readUInt40LE (str : string , index? : integer ): integer
Reads unsigned 5-byte (40 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 1099511627775
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 5-byte integer.
function pack.readUInt48BE (str : string , index? : integer ): integer
Reads unsigned 6-byte (48 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 281474976710655
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 6-byte integer.
function pack.readUInt48LE (str : string , index? : integer ): integer
Reads unsigned 6-byte (48 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 281474976710655
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 6-byte integer.
function pack.readUInt56BE (str : string , index? : integer ): integer
Reads unsigned 7-byte (56 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 7-byte integer.
function pack.readUInt56LE (str : string , index? : integer ): integer
Reads unsigned 7-byte (56 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 7-byte integer.
function pack.readUInt64BE (str : string , index? : integer ): integer
Reads unsigned 8-byte (64 bit) integer from big endian bytes of the string.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 8-byte integer.
function pack.readUInt64LE (str : string , index? : integer ): integer
Reads unsigned 8-byte (64 bit) integer from little endian bytes of the string.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 8-byte integer.
function pack.readUInt8 (str : string , index? : integer ): integer
Reads unsigned 1-byte (8 bit) integer from the string.
Valid values without loss of precision: 0 - 255
Parameters
Name Type Description strstring The string to read from. indexoptional integer The index of the first byte (1-based), 1 by default.
Returns
Type Name Description integer valueThe unsigned 1-byte integer.
# pack.readUnsignedFixedPoint
function pack.readUnsignedFixedPoint (str : string , bit_position? : integer , m : integer , n : integer , is_little_endian? : boolean ): number
Reads unsigned fixed-point number (m integer bits and n fractional bits) from any bit position of the string.
Parameters
Name Type Description strstring The string to read from. bit_positionoptional integer The position of the first bit (0-based), 0 by default. minteger The number of integer bits. ninteger The number of fractional bits. is_little_endianoptional boolean true for little endian, false by default.
Returns
Type Name Description number valueThe unsigned fixed-point number.
function pack.writeCountedString (data : string , length_size : integer , is_little_endian? : boolean ): string
Writes the string prefixed with its length.
Throws an error if the string is too long for the length size.
lua Copy
writeCountedString ( "abc" , 1 ) --> "\3abc"
Parameters
Name Type Description datastring The string to write. length_sizeinteger The size of the length in bytes (1 - 8). is_little_endianoptional boolean true to write the length as little endian, false by default.
Returns
Type Name Description string dataThe counted string.
function pack.writeDateBE (day? : integer , month? : integer , year? : integer ): string
Writes 2-byte (16 bit) DOS date as big endian bytes.
Parameters
Name Type Description dayoptional integer The day. monthoptional integer The month. yearoptional integer The year.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeDateLE (day? : integer , month? : integer , year? : integer ): string
Writes 2-byte (16 bit) DOS date as little endian bytes.
Parameters
Name Type Description dayoptional integer The day. monthoptional integer The month. yearoptional integer The year.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeDoubleBE (value : number ): string
Writes double precision (8-byte, 64 bit) floating point number as big endian bytes.
Parameters
Name Type Description valuenumber The floating point number.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeDoubleLE (value : number ): string
Writes double precision (8-byte, 64 bit) floating point number as little endian bytes.
Parameters
Name Type Description valuenumber The floating point number.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeFixedString (data : string , length : integer ): string
Writes the string of a fixed size, shorter data is padded with \0 and longer data is cut.
lua Copy
writeFixedString ( "abc" , 5 ) --> "abc\0\0"
writeFixedString ( "abcdef" , 5 ) --> "abcde"
Parameters
Name Type Description datastring The string to write. lengthinteger The size of the string in bytes.
Returns
Type Name Description string dataThe length-byte string.
function pack.writeFloatBE (value : number ): string
Writes single precision (4-byte, 32 bit) floating point number as big endian bytes.
Parameters
Name Type Description valuenumber The floating point number.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeFloatLE (value : number ): string
Writes single precision (4-byte, 32 bit) floating point number as little endian bytes.
Parameters
Name Type Description valuenumber The floating point number.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeInt16BE (value : integer ): string
Writes signed 2-byte (16 bit) integer as big endian bytes.
Valid values without loss of precision: -32768 - 32767
Parameters
Name Type Description valueinteger The signed 2-byte integer.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeInt16LE (value : integer ): string
Writes signed 2-byte (16 bit) integer as little endian bytes.
Valid values without loss of precision: -32768 - 32767
Parameters
Name Type Description valueinteger The signed 2-byte integer.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeInt24BE (value : integer ): string
Writes signed 3-byte (24 bit) integer as big endian bytes.
Valid values without loss of precision: -8388608 - 8388607
Parameters
Name Type Description valueinteger The signed 3-byte integer.
Returns
Type Name Description string dataThe 3-byte string.
function pack.writeInt24LE (value : integer ): string
Writes signed 3-byte (24 bit) integer as little endian bytes.
Valid values without loss of precision: -8388608 - 8388607
Parameters
Name Type Description valueinteger The signed 3-byte integer.
Returns
Type Name Description string dataThe 3-byte string.
function pack.writeInt32BE (value : integer ): string
Writes signed 4-byte (32 bit) integer as big endian bytes.
Valid values without loss of precision: -2147483648 - 2147483647
Parameters
Name Type Description valueinteger The signed 4-byte integer.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeInt32LE (value : integer ): string
Writes signed 4-byte (32 bit) integer as little endian bytes.
Valid values without loss of precision: -2147483648 - 2147483647
Parameters
Name Type Description valueinteger The signed 4-byte integer.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeInt40BE (value : integer ): string
Writes signed 5-byte (40 bit) integer as big endian bytes.
Valid values without loss of precision: -549755813888 - 549755813887
Parameters
Name Type Description valueinteger The signed 5-byte integer.
Returns
Type Name Description string dataThe 5-byte string.
function pack.writeInt40LE (value : integer ): string
Writes signed 5-byte (40 bit) integer as little endian bytes.
Valid values without loss of precision: -549755813888 - 549755813887
Parameters
Name Type Description valueinteger The signed 5-byte integer.
Returns
Type Name Description string dataThe 5-byte string.
function pack.writeInt48BE (value : integer ): string
Writes signed 6-byte (48 bit) integer as big endian bytes.
Valid values without loss of precision: -140737488355328 - 140737488355327
Parameters
Name Type Description valueinteger The signed 6-byte integer.
Returns
Type Name Description string dataThe 6-byte string.
function pack.writeInt48LE (value : integer ): string
Writes signed 6-byte (48 bit) integer as little endian bytes.
Valid values without loss of precision: -140737488355328 - 140737488355327
Parameters
Name Type Description valueinteger The signed 6-byte integer.
Returns
Type Name Description string dataThe 6-byte string.
function pack.writeInt56BE (value : integer ): string
Writes signed 7-byte (56 bit) integer as big endian bytes.
Valid values without loss of precision: -36028797018963968 - 36028797018963967
Parameters
Name Type Description valueinteger The signed 7-byte integer.
Returns
Type Name Description string dataThe 7-byte string.
function pack.writeInt56LE (value : integer ): string
Writes signed 7-byte (56 bit) integer as little endian bytes.
Valid values without loss of precision: -36028797018963968 - 36028797018963967
Parameters
Name Type Description valueinteger The signed 7-byte integer.
Returns
Type Name Description string dataThe 7-byte string.
function pack.writeInt64BE (value : integer ): string
Writes signed 8-byte (64 bit) integer as big endian bytes.
Valid values without loss of precision: -9007199254740991 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The signed 8-byte integer.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeInt64LE (value : integer ): string
Writes signed 8-byte (64 bit) integer as little endian bytes.
Valid values without loss of precision: -9007199254740991 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The signed 8-byte integer.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeInt8 (value : integer ): string
Writes signed 1-byte (8 bit) integer as a string.
Valid values without loss of precision: -128 - 127
Parameters
Name Type Description valueinteger The signed 1-byte integer.
Returns
Type Name Description string dataThe 1-byte string.
# pack.writeLineTerminatedString
function pack.writeLineTerminatedString (data : string ): string
Writes the line, the string followed by \n.
Throws an error if the string contains \n.
Parameters
Name Type Description datastring The string to write.
Returns
Type Name Description string dataThe line.
# pack.writeNullTerminatedString
function pack.writeNullTerminatedString (data : string ): string
Writes C-style string, the string followed by the null terminator (\0).
Throws an error if the string contains \0.
Parameters
Name Type Description datastring The string to write.
Returns
Type Name Description string dataThe null terminated string.
# pack.writeTerminatedString
function pack.writeTerminatedString (data : string , terminator : integer ): string
Writes the string followed by the terminator byte.
Throws an error if the string contains the terminator.
Parameters
Name Type Description datastring The string to write. terminatorinteger The terminator byte (0 - 255).
Returns
Type Name Description string dataThe terminated string.
function pack.writeTimeBE (hours? : integer , minutes? : integer , seconds? : integer ): string
Writes 2-byte (16 bit) DOS time as big endian bytes.
Parameters
Name Type Description hoursoptional integer The number of hours. minutesoptional integer The number of minutes. secondsoptional integer The number of seconds, will be rounded .
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeTimeLE (hours? : integer , minutes? : integer , seconds? : integer ): string
Writes 2-byte (16 bit) DOS time as little endian bytes.
Parameters
Name Type Description hoursoptional integer The number of hours. minutesoptional integer The number of minutes. secondsoptional integer The number of seconds, will be rounded .
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeUInt16BE (value : integer ): string
Writes unsigned 2-byte (16 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 65535
Parameters
Name Type Description valueinteger The unsigned 2-byte integer.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeUInt16LE (value : integer ): string
Writes unsigned 2-byte (16 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 65535
Parameters
Name Type Description valueinteger The unsigned 2-byte integer.
Returns
Type Name Description string dataThe 2-byte string.
function pack.writeUInt24BE (value : integer ): string
Writes unsigned 3-byte (24 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 16777215
Parameters
Name Type Description valueinteger The unsigned 3-byte integer.
Returns
Type Name Description string dataThe 3-byte string.
function pack.writeUInt24LE (value : integer ): string
Writes unsigned 3-byte (24 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 16777215
Parameters
Name Type Description valueinteger The unsigned 3-byte integer.
Returns
Type Name Description string dataThe 3-byte string.
function pack.writeUInt32BE (value : integer ): string
Writes unsigned 4-byte (32 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 4294967295
Parameters
Name Type Description valueinteger The unsigned 4-byte integer.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeUInt32LE (value : integer ): string
Writes unsigned 4-byte (32 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 4294967295
Parameters
Name Type Description valueinteger The unsigned 4-byte integer.
Returns
Type Name Description string dataThe 4-byte string.
function pack.writeUInt40BE (value : integer ): string
Writes unsigned 5-byte (40 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 1099511627775.
Parameters
Name Type Description valueinteger The unsigned 5-byte integer.
Returns
Type Name Description string dataThe 5-byte string.
function pack.writeUInt40LE (value : integer ): string
Writes unsigned 5-byte (40 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 1099511627775.
Parameters
Name Type Description valueinteger The unsigned 5-byte integer.
Returns
Type Name Description string dataThe 5-byte string.
function pack.writeUInt48BE (value : integer ): string
Writes unsigned 6-byte (48 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 281474976710655
Parameters
Name Type Description valueinteger The unsigned 6-byte integer.
Returns
Type Name Description string dataThe 6-byte string.
function pack.writeUInt48LE (value : integer ): string
Writes unsigned 6-byte (48 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 281474976710655
Parameters
Name Type Description valueinteger The unsigned 6-byte integer.
Returns
Type Name Description string dataThe 6-byte string.
function pack.writeUInt56BE (value : integer ): string
Writes unsigned 7-byte (56 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The unsigned 7-byte integer.
Returns
Type Name Description string dataThe 7-byte string.
function pack.writeUInt56LE (value : integer ): string
Writes unsigned 7-byte (56 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The unsigned 7-byte integer.
Returns
Type Name Description string dataThe 7-byte string.
function pack.writeUInt64BE (value : integer ): string
Writes unsigned 8-byte (64 bit) integer as big endian bytes.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The unsigned 8-byte integer.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeUInt64LE (value : integer ): string
Writes unsigned 8-byte (64 bit) integer as little endian bytes.
Valid values without loss of precision: 0 - 9007199254740991
All values above will have problems when working with them.
Parameters
Name Type Description valueinteger The unsigned 8-byte integer.
Returns
Type Name Description string dataThe 8-byte string.
function pack.writeUInt8 (value : integer ): string
Writes unsigned 1-byte (8 bit) integer as a string.
Valid values without loss of precision: 0 - 255
Parameters
Name Type Description valueinteger The unsigned 1-byte integer.
Returns
Type Name Description string dataThe 1-byte string.