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
-
Did you try just printing it first?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年10月25日 06:29:17 +00:00Commented Oct 25, 2017 at 6:29
-
1That's a completely different question from what you're asking.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年10月25日 06:33:20 +00:00Commented Oct 25, 2017 at 6:33
-
2I'm voting to close this question as off-topic because very elemental programming question unrelated to Arduinouser31481– user314812017年10月25日 07:07:30 +00:00Commented Oct 25, 2017 at 7:07
-
1I 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.Majenko– Majenko2017年10月25日 10:05:45 +00:00Commented Oct 25, 2017 at 10:05
-
1The whole idea of "add a prefix to a byte variable and store it in the byte variable" is completely meaningless.Majenko– Majenko2017年10月25日 10:19:50 +00:00Commented Oct 25, 2017 at 10:19
3 Answers 3
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.
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;
}
-
You forgot to advance
input
and check if it's0円
before shifting.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年10月25日 08:17:53 +00:00Commented Oct 25, 2017 at 8:17 -
Why are you using:
input <<= 4;
on pointer?KIIV– KIIV2017年10月25日 08:32:02 +00:00Commented Oct 25, 2017 at 8:32 -
1@KIIV because I was far too hasty...ratchet freak– ratchet freak2017年10月25日 08:39:11 +00:00Commented Oct 25, 2017 at 8:39
-
I think you should use:
result = result<<4 + ...
or at least move that bitshift before theresult += ...
.KIIV– KIIV2017年10月25日 08:45:16 +00:00Commented Oct 25, 2017 at 8:45
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.