user1437099
user1437099

Reputation: 39

Store hex value as string

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);
    Serial.println(CardID);
  }

But the Sting is stored as CardID = 16161616

Upvotes: 2

Views: 12163

Answers (5)

Hzz
Hzz

Reputation: 1928

So simple in Arduino Programming Language to convert hex to be a string.

Just doing:

char StringValue[ ]=String(0xFF,HEX)

Upvotes: 0

fandyushin
fandyushin

Reputation: 2432

#include <stdio.h>

int main(void) {
  int nHex = 0xC697C63B;
  char pHexStr[100];
  sprintf(pHexStr,"%x",nHex);
  fprintf(stdout, "%s", pHexStr);

  return 0;
}

Upvotes: 0

taarraas
taarraas

Reputation: 1483

You can use c-style sprintf :

char str[100];
sprintf(str, "%08x", HEX);

Upvotes: 0

Youka
Youka

Reputation: 2705

std::ostringstream + IO manipulator hex may be want you want.

Upvotes: 0

cdonat
cdonat

Reputation: 2822

You should use an ostringstream:

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

Upvotes: 2

Related Questions