Byte Packing¶
-
knPack(type:
string, value:number):string¶ Convert the given
valueinto the giventype, then return its representation as bytes. The endianness is always that of the host processor, which is always little endian for all of KnShim’s supported architectures. This is most useful withknPatch().The
typeis one of the following strings:float: single precision (32 bit) floating point numberdouble: double precision (64 bit) floating point numbercharorbool: a single 8-bit byteshort: a 16-bit signed integerint: a 32-bit signed integerlong: a 64-bit signed integerpointer: a pointer, size depending on platform
The
valuemay be any Lua value, though only some will convert in a sensible manner. One notable example is pointers, which take only a light userdata as their input.Added in version 19: Pointers
-
knUnpack(type:
string, data:string):number¶ Essentially the reverse of
knPack(), taking data bytes and converting it to a number or integer value.Added in version 19.
-
knHexToBin(hexdata:
string):string¶ Convert a string of hexidecimal digits representing bytes to a string of those bytes. Non-hexdigit characters in the string (e.g. anything that is not
0-9,a-forA-F) are ignored.As an example, this converts a string representing the AArch64 ret instruction into the raw bytes and returns it:
local ret = knHexToBin("c0 03 5f d6")
Tip
You can create an alias of this function and use Lua’s special single-string-argument call syntax to create an elegant way of writing hex data:
hex = knHexToBin local ret = hex "c0 03 5f d6"
Added in version 20.
-
knBinToHex(data:
string):string¶ Converts binary data into a string of hexdigits. This string does not contain any extranious characters for formatting, like spaces or tabs.
The result of this function is suitable to be passed back into
knHexToBin()to retrieve the same data again.Added in version 20.