Skip to content

Bytes Conversions

ResIOT allows many conversions between different value types.
The following is a quick example of many.

    -- Convert a single Byte into String | result = {"รต"}
    result = resiot_b2str(0xF5)

    -- Convert a Byte Array into String | result = {"PRU"}
    result = resiot_ba2str({0x50, 0x52, 0x55})

    -- Convert a Byte Array into a Hexadecimal String | result = "215487"
    result = resiot_hexencode({0x21, 0x54, 0x87})

    -- Convert a Hexadecimal String into a Byte Array | 
    -- result = {0x48 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64 0x21}
    result = resiot_hexdecode("48656C6C6F20776F726C6421")

    -- Convert an Integer value into a Byte Array with length N
    -- 24 = 0x18
    Value = 24
    -- result = {0x00, 0x18}
    result = resiot_i2nbl(Value, 2)

    -- Convert an Integer into Byte | result = {0x14}
    result = resiot_int2byte(20)

    -- Convert a Bytes Array into an Integer | result = 255
    result = resiot_ba2int({0x00, 0xFF})

    -- Convert a Float32 type into a Byte Array | result = {0x00, 0x00, 0xC0, 0x3F}
    result = resiot_float322ba(1.5)

    -- Convert a Byte Array into a Float32 | result = 1.5
    Value = resiot_ba2float32({0x00, 0x00, 0xC0, 0x3F})