user1437099 user1437099 - 1 year ago 222
C++ Question

Store hex value as string (Arduino project)

I'm working on an Arduino project and I want to store a hex value as string.

eg: Hex is C697C63B

for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i], HEX);

I would like to store the String as
x = "C697C63B";

String CardID = "";
for (byte i = 0; i < bufferSize; i++) {
CardID += (buffer[i],HEX);

But the Sting is stored as
CardID = 16161616

Sorry I just start C++ for a week and I spend 3 day already just to find answer.

Okay i found the answer now thank you everyone that help

String CardID = "";
for (byte i = 0; i < bufferSize; i++)
CardID += String(buffer[i], HEX);

Answer Source

You should use an ostringstream:

auto outstr = std::ostringstream{};
outstr << std::hex << 0xC697C63Bul;
auto myHexString = outstr.str();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download