-1

I want to convert a decimal value stored in a string into a hex value and store it in a byte variable. How can I do that?

eg

std::string DispalyAddress = params.substr(0,2);

The result is 12.

Now I want to convert 12 into hex ie C and store it in a byte variable. Something like

byte MyAddress = (byte)DispalyAddress.c_str();
user2837961
  • 1,505
  • 3
  • 27
  • 67

1 Answers1

1

Whatever you byte type is (I assume it's char or unsigned char), and if I correctly understood your question:

If you can use C++11, then do

byte MyAddress = (byte)std::stoi(DisplayAddress, 0, 16);

if not, then

byte MyAddress = (byte)strtol(DisplayAddress.c_str(), NULL, 16);

This will store value 18 (0x12) in MyAddress.

Paul
  • 13,042
  • 3
  • 41
  • 59