-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

asked Oct 25, 2017 at 6:28
8
  • Did you try just printing it first? Commented Oct 25, 2017 at 6:29
  • 1
    That's a completely different question from what you're asking. Commented Oct 25, 2017 at 6:33
  • 2
    I'm voting to close this question as off-topic because very elemental programming question unrelated to Arduino Commented Oct 25, 2017 at 7:07
  • 1
    I think there is a serious basic misunderstanding of very elementary C (and even basic programming and even "what a computer is") concepts here. You need to go right back to basics and learn how to count (metaphorically) before you start tacking calculus. Commented Oct 25, 2017 at 10:05
  • 1
    The whole idea of "add a prefix to a byte variable and store it in the byte variable" is completely meaningless. Commented Oct 25, 2017 at 10:19

3 Answers 3

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.

answered Oct 25, 2017 at 9:14
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,6521 gold badge17 silver badges31 bronze badges
answered Oct 25, 2017 at 8:14
4
  • You forgot to advance input and check if it's 0円 before shifting. Commented Oct 25, 2017 at 8:17
  • Why are you using: input <<= 4; on pointer? Commented Oct 25, 2017 at 8:32
  • 1
    @KIIV because I was far too hasty... Commented Oct 25, 2017 at 8:39
  • I think you should use: result = result<<4 + ... or at least move that bitshift before the result += .... Commented Oct 25, 2017 at 8:45
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.

answered Oct 25, 2017 at 9:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.