Binary Packing, Bits & Hex
Round-trips data through the bin conversion functions: packing a bit array
to bytes with "RBITS", converting between binary and hex text with
bin.toHex/bin.fromHex, and packing/unpacking word-swapped 32-bit floats
("F32" + "BADC").
print("==== Convert an array of 16 bits to a binary string of two bytes ====");
{
let bitArray = [ 1,0,0,0,0,0,0,0, 0,1,1,1,1,1,1,1 ]; // true or false could be used
let bytes = bin.fromArray(bitArray,"RBITS");
let hex = bin.toHex(bytes);
print("raw bit array:", string(bitArray) );
print("packed to bytes: ", hex); // 01fe
}
print("");
print("==== Convert two bytes from a hex string into an array of bits ====");
{
let bytes = bin.fromHex("01fe");
let bitArray = bin.toArray(bytes,"RBITS");
print("raw hex: ", bin.toHex(bytes));
print("converted to array: ", string(bitArray));
}
print("");
print("==== Convert an array of numbers to a 32 bit floating point packed data structure ====");
{
let numberArray = [ 12345.6789,100,200,300,400 ];
let bytes = bin.fromArray(numberArray,"F32","BADC");
let hex = bin.toHex(bytes);
print("raw number array:", string(numberArray) );
print("packed to bytes: ", hex);
}
print("");
print("==== Convert packed 32 bit floating point numbers to an array ====");
{
let hex = "e6b74640000042c80000434800004396000043c8"; // Hex output from previous run
let bytes = bin.fromHex(hex);
let numberArray = bin.toArray(bytes,"F32","BADC");
print("raw hex: ", hex);
print("converted to array: ", string(numberArray));
}