Skip to content

Byte and String Handling

Name Input Params Return Data Description
resiot_appbyte BYTE [ ], BYTE BYTE [ ] Appends a byte at the end of a byte array
resiot_apps2ba BYTE [ ], String BYTE [ ] Appends a string at the end of a byte array
resiot_b2str BYTE String Convert a single byte into a String
resiot_ba2str BYTE [ ] String Convert array bytes into String
resiot_base64encode BYTE [ ] base64 String Encode array bites into a base64 String
resiot_base64decode base64 String BYTE [ ] Decode an input string into a Byte array
resiot_baslice BYTE [ ], Integer, Integer BYTE [ ] Slice the array bytes from a starting index Start to an ending index End
resiot_byarrlen BYTE [ ] Integer Calculate the array bytes lenght
resiot_bytearrayconcat BYTE [ ], BYTE [ ], BYTE [ ] BYTE [ ]???todo Concatenate all the extra bytes arrays BytesN passed with the main one MainBytes
resiot_crc1145 BYTE [ ] ???todo Calculate the Checksum of the bytes array passed in
resiot_hexencode BYTE [ ] String Convert an input bytes array into a hexadecimal string
resiot_i2nbl Integer, Integer BYTE [ ] Convert an Integer value into a byte of length n
resiot_int2byte Integer BYTE [ ] Convert an Integer value
resiot_ba2int BYTE [ ] Integer Convert an input bytes array into an Integer
resiot_readbit BYTE [ ], Integer Boolean Check if the Byte at Index is 0 or 1

resiot_appbyte(BYTE [ ], BYTE)

Appends a byte at the end of a byte array
Example
ArrReturn = resiot_appbyte(ArrByte,Byte)
Input Parameters
 - ArrByte (BYTE [ ]): the original byte array
 - Byte (BYTE): a byte to append at the end of ArrByte
Returns
 - ArrReturn (BYTE [ ]): A new array with Byte appended to ArrByte

local payload = {0xB3, 0xC2, 0xFF}
Byte = 0xC5
payload = resiot_appbyte(payload, Byte)
-- payload bytes array contains {0xB3, 0xC2, 0xFF, 0xC5}

Return to index


resiot_apps2ba (BYTE [ ], String)

Appends a string at the end of a byte array
Example
ArrReturn = resiot_apps2ba(ArrByte, S)
Input Parameters
 - ArrByte (BYTE [ ]): the original byte array
 - S (String): a string to append at the end of ArrByte
Returns
 - ArrReturn (BYTE [ ]): The function returns a new array with S appended to ArrByte

local messagepayload = {"a", "b", "c"}
String = "d"
messagepayload = resiot_apps2ba(messagepayload, String)
-- messagepayload string array contains {"a", "b", "c", "d"}

Return to index


resiot_b2str(BYTE)

Convert a single byte into a String
Example
StringReturn = resiot_b2str(Byte)
Input Parameters
 - Byte (BYTE): the byte to convert
Returns
 - StringReturn: The function returns the single byte converted into a String.

Byte = 0xF5
local byteconverted = {}
byteconverted = resiot_b2str(Byte)
-- byteconverted = {"õ"}

Return to index


resiot_ba2str(BYTE [ ])

Convert array bytes into String
Example
StringReturn = resiot_ba2str(ArrByte)
Input Parameters
 - ArrByte (BYTE [ ]): the bytes to convert
Returns
 - StringReturn: The function returns the array bytes converted into String

Byte = {0x50, 0x052, 0x55}
local byteconverted = {}
byteconverted = resiot_ba2str(Byte)
-- byteconverted = {"PRU"}

Return to index


resiot_base64encode(BYTE [ ])

Encode array bites into a base64 String
Example
StringReturn = resiot_base64encode(ArrByte)
Input Parameters
 - ArrByte (BYTE [ ]): the bytes to encode Returns
 - StringReturn: The function returns the array bytes encoded into a base64 String

Byte = {0x50, 0x052, 0x55}
local byteconverted = {}
byteconverted = resiot_base64encode(Byte)
-- byteconverted = {"EAVC"}

Return to index


resiot_base64decode(String)

Decode an input string into a Byte array
Example
Byte, error = resiot_base64decode(input)
Input Parameters
 - input String: the string to decode
Returns
 - Byte (BYTE [ ]): The function returns string decoded into a Byte array
 - Error (String): The error if it occurs

a, b = resiot_base64decode("SGVsbG8gd29ybGQh")
if b ~= "" then
    resiot_debug("UNABLE TO DECODE: " .. b)
else
    resiot_debug(a)
end
-- prints "Hello world!"

Return to index


resiot_baslice(BYTE [ ], Integer, Integer)

Slice the array bytes from a starting index Start to an ending index End
Example
ArrReturn = resiot_baslice(Byte, Start, End)
Input Parameters
 - Byte (BYTE [ ]): the bytes array to slice
 - Start (Integer): the int index where the slice start
 - End (Integer): the int index where the slice end
Returns
 - ArrReturn: The function returns the array bytes sliced from Start to End

Byte = {0x50, 0x52, 0x55, 0x33, 0x97, 0x85}
local SlicedByte = {}
SlicedByte = resiot_baslice(Byte, 1, 3)
-- SlicedByte = {0x52, 0x55}

Return to index


resiot_byarrlen(BYTE [ ])

Calculate the array bytes lenght
Example
IntReturn = resiot_byarrlen(Byte)
Input Parameters
 - Byte (BYTE [ ]): the bytes array
Returns
 - IntReturn: The function returns the array bytes length as int

Byte = {0x50, 0x43, 0x31}
local ArrayLength = {}
ArrayLength = resiot_byarrlen(Byte)
-- ArrayLength = {3}

Return to index


resiot_bytearrayconcat(BYTE [ ], BYTE [ ], BYTE [ ])

Concatenate all the extra bytes arrays BytesN passed with the main one MainBytes
Example
ArrReturn = resiot_bytearrayconcat(MainByte, Bytes1, Bytes2, BytesN)
Input Parameters
 - MainBytes (BYTE [ ]): the main bytes array
 - BytesN (BYTE [ ]): all the bytes arrays passed in
Returns
 - ArrReturn: The function returns the concatenation of all the bytes arrays passed in BytesN with the main bytes array MainBytes as an array of bytes

MainByte = {0x50}
Byte1 = {0x54}
Byte2 = {0x12}
Byte3 = {0x76}
local ConcatBytes = {}
ConcatBytes = resiot_bytearrayconcat(MainByte, Byte1, Byte2, Byte3)
-- ConcatBytes = {0x50, 0x54, 0x12, 0x76}

Return to index


resiot_crc1145(BYTE [ ])

Calculate the Checksum of the bytes array passed in
Example
ArrReturn = resiot_crc1145(ArrByte)
Input Parameters
 - ArrByte (BYTE [ ]): the bytes array
Returns
 - ArrReturn: The function returns the Checksum of the bytes array passed in Byte as a RFC 1145 standard in a bytes array

Byte = {0x43}
local Checksum = {}
Checksum = resiot_crc1145(Byte)
-- Checksum = {yC} --> Converted in String

Return to index


resiot_hexencode(BYTE [ ])

Convert an input bytes array into a hexadecimal string
Example
StringReturn = resiot_hexencode(ArrByte)
Input Parameters
 - ArrByte (BYTE [ ]): the bytes array to convert
Returns
 - StringReturn (String): The function returns the input bytes array ArrByte converted into a hexadecimal string

BytesArray = {0x21, 0x54, 0x87}
ConvertedBytes = {}
ConvertedBytes = resiot_hexencode(BytesArray)
-- ConvertedBytes now contains "2496744ad485c"

Return to index


resiot_i2nbl(Integer, Integer)

Convert an Integer value into a byte of length n
Example
ByteReturn = resiot_i2nbl(Int, n)
Input Parameters
 - Int (Integer): the Integer value
 - n (Integer): the length of the byte
Returns
 - ByteReturn (BYTE [ ]): The value of Int convert into a byte of length n

local temp = 246
length = 2
retbyte = resiot_i2nbl(temp, length)
-- retbyte  contains {0xB3}

Return to index


resiot_int2byte(Integer)

Convert an Integer value
Example
ByteReturn = resiot_int2byte(Int)
Input Parameters
 - Int (Integer): the Integer value
Returns
 - ByteReturn (BYTE [ ]): The value of Int convert into a byte

local temp = 246
byte = resiot_int2byte(temp)
-- byte = {0xB3}

Return to index


resiot_ba2int(BYTE [ ])

Convert an input bytes array into an Integer
Example
value = resiot_ba2int(ArrByte)
Input Parameters
 - ArrByte (BYTE [ ]): the bytes array to convert
Returns
 - Value (Integer): The value of ArrByte convert into an Integer

Byte = {0x0, 0xFF}
value = resiot_ba2int(Byte)
-- value = 255

Return to index


resiot_readbit(BYTE, Integer)

Check if the Byte at Index is 0 or 1
Example
BoolReturn = resiot_readbit(Byte, Index)
Input Parameters
 - Byte (BYTE): the byte
 - Index (Int): the index of the byte to check It must be between 0 and 7
Returns
 - BoolReturn (Boolean): The function returns true if the checked byte is 1 or false if it is 0

local Byte = 0x00
readBit = resiot_readbit(Byte, 2)
-- readBit = false 

Return to index