Store Hex Value as String (Arduino Project)

Store hex value as string (Arduino project)

You should use an ostringstream:

auto outstr = std::ostringstream{};
outstr << std::hex << 0xC697C63Bul;
auto myHexString = outstr.str();

Convert integer/decimal to hex on an Arduino?

Take a look at the Arduino String tutorial here. The code below was taken from that example.

// using an int and a base (hexadecimal):
stringOne = String(45, HEX);
// prints "2d", which is the hexadecimal version of decimal 45:
Serial.println(stringOne);

There are plenty of other examples on that page, though I think for floating point numbers you'll have to roll your own.

Arduino: Convert hex string to uint64 or decimal string

You can make your own implementation :

#include <stdio.h>
#include <stdint.h>
#include <ctype.h>

uint64_t getUInt64fromHex(char const *str)
{
uint64_t accumulator = 0;
for (size_t i = 0 ; isxdigit((unsigned char)str[i]) ; ++i)
{
char c = str[i];
accumulator *= 16;
if (isdigit(c)) /* '0' .. '9'*/
accumulator += c - '0';
else if (isupper(c)) /* 'A' .. 'F'*/
accumulator += c - 'A' + 10;
else /* 'a' .. 'f'*/
accumulator += c - 'a' + 10;

}

return accumulator;
}

int main(void)
{
printf("%llu\n", (long long unsigned)getUInt64fromHex("43a2be2a42380"));
return 0;
}

Comparing chars with hex values

Plain char can be signed or unsigned. If the type is unsigned, then all works as you'd expect.
If the type is signed, then assigning 0xFF to c1 means that the value will be promoted to -1 when the comparison is executed, but the 0xFF is a regular positive integer, so the comparison of -1 == 0xFF fails.

Note that the types char, signed char and unsigned char are distinct, but two of them have the same representation (and one of the two is char).



Related Topics



Leave a reply



Submit