-1

I have a byte variable CardNumberByte with a value of E8 if i print out as HEX:

Serial.println(CardNumberByte, HEX)

which returns E8.

What I would like to do is to "append 0x as prefix" so that it is equivalent to

Char CardNumberByte=0xE8;

how should i concatenate 0x to CardNumberByte?

here is the reason i need to do so.

Thanks

3 Answers3

2

I've looked at you question and the question you linked two and I've tried to join the dots to give you an answer, so I might be well off the mark, but...

I think you might have misunderstood what 0x28 means.

0x?? means that the ?? represent a hexadecimal number rather than a decimal number.

So 10 (Ten) in decimal is the same as 0x0A in hex or B00001010 or in binary or even 012 in octal. (See https://www.arduino.cc/en/Reference/IntegerConstants for more details)

So you want to have some code that says:

char Address = 0x28;

This is assigning the hexadecimal value 28 to the variable Address. You could also write:

char Address = 40;

In this case you are assigning the value decimal 40 (which is hex 28) to the variable. If you want to write strange code that is misleading you could write:

char Address = '(';  // ( is ASCII character 0x28 or 40 - Don't do it like this!

One other important thing here is that the char type in this case is not being used as type that holds a printable character, its being used as a signed 8 bit number.

Code Gorilla
  • 5,637
  • 1
  • 15
  • 31
0

The answer to the question you asked is:

Serial.print("0x");
Serial.println(CardNumberByte, HEX);

But what you want to do according to the comment you posted is parse "E8" to give you 0xE8.

int parseHex(char* input){
    int result = 0;
    while(input && *input){
        if(*input >= '0' && *input <= '9'){
            result <<= 4;
            result+= *input - '0';
        }
        if(*input >= 'a' && *input <= 'f'){
            result <<= 4;
            result+= *input - 'a'+10;
        }
        if(*input >= 'A' && *input <= 'F'){
            result <<= 4;
            result+= *input - 'A'+10;
        } 
        input++;
    }
    return result;
}
Code Gorilla
  • 5,637
  • 1
  • 15
  • 31
ratchet freak
  • 3,267
  • 1
  • 12
  • 12
0

Here you have:

  char buffer[20];
  snprintf(buffer, sizeof(buffer), "0x%X", 1234);
  Serial.println(buffer);

You have to study the classic C print functions.