Have you ever wanted to transmit int
,short
,long
,double
or any other numeric type over I2C,SPI,serial or other protocol or bus, but you converted variable to string to be able to transmit it char by char. This library enables you to convert any numeric value to bytes or other way around and you can also print array of bytes.
When you are using this library, you need to consider variable byte size, because if you are using different platforms, then there may be some errors, because int on platform 1 has 4 bytes and int on platform 2 may has 2 bytes.
Convert numeric variable for eg. int
,short
,float
,double
to array of bytes.
int somevar = 5;
size_t blk_size = 0;
uint8_t *block = ByteConvert::varToArray<int>(blk_size,somevar);
// Use array
delete block; // Don't forget to free memory, when you don't need array any more
Convert array of bytes to numeric variable.
uint8_t *block; // Predefined byte array with size of int
int somevar = ByteConvert::arrayToVar<int>(block);
// Use block & somevar
delete block; // Don't forget to free memory, when you don't need array any more
// Use somevar
Convert array of bytes to string of hex characters
size_t blk_size; // Predefined size of byte array
uint8_t *block; // Predefined byte array with size of int
String somevar = ByteConvert::arrayToString(blk_size,block);
// Use block & somevar
delete block; // Don't forget to free memory, when you don't need array any more
// Use somevar
Convert string of hex characters to array of bytes
String somevar = ""; // Predefined string
size_t blk_size = 0;
uint8_t *block = ByteConvert::stringToArray(blk_size,somevar);
// Use block
delete block; // Don't forget to free memory, when you don't need array any more