Hi Guys,
I have a mac address that is going into a function as a String. I need to turn this mac address into bytes so that I can store this in the EEPROM.
The String is coming in as "EA" and I need this to be transforms to hex "0xEA".
What would be the best way to do this?
If you use c-strings (not String with capital S), strtoul() comes to mind.
It would be useful to give a full example of the input data and what you want the output data to look like. Also, most of us here would discourage using the String data type and suggest converting the input to a char array before processing.
The String is coming in as "EA" and I need this to be transforms to hex "0xEA".
On the Monitor of the PC, we have EA (we see ASCII characters and not hex digit E or A).
EA (what we see on the Monitor) is existing as : 01000101 and 01000001 (the ASCII bits). Now, to get EA (two hex digits = 11101010), apply the following algorithm on the ASCII bits.
(1) Subtract 0x37 if the ASCII code is in the range of 0x41 - 0x46; else, subtract 0x30
Accordingly, we get:
a. 0x45 (01000101 for 'E') - 0x37 = 0x0E = 00001110
b. 0x41 (01000001 for 'A') - 0x37 = 0x0A = 00001010
(2) Shift (1)a(0x0E) to the left by 4-bit; we get: 11100000
(3) Make an OR operation between (2) and (1)b; we get: 11101010 = EAh = 0xEA
hantoo:
The String is coming in as "EA" and I need this to be transforms to hex "0xEA".
Understand that this question has issues to several of us and is very ambiguous since
- the word String has a precise meaning
- in C and C++ "EA" and "0xEA" are both strings but neither is a String and neither is a integer type like an unsigned char.
- The term hex is ambiguous as it doesn't indicate a actual data type so it isn't clear what you mean by "0xEA" since that is a string.
- mac addresses are more than a single byte but you have only shown a single byte of of the mac address in the two strings "EA" and "0xEA" that you showed us.
While we might be able to guess what you mean, it would be much better if you would clarify things as it will save time from people making incorrect assumptions and providing suggestions that don't really apply to what you are wanting to do and the actual data format or type being used.
As sterretje hinted at and as econjack pointed out, we need to know the actual/real data types involved to understand the actual input and output data types and format.
--- bill