Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ascii to Hex conversion

Status
Not open for further replies.

srinathvarda

Newbie level 6
Joined
Sep 15, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
hyderabad
Activity points
1,352
Hi Guys,

I have trying to solve this problem since 4 days, but I'm not going anywhere, perhaps someone can help me here.

I have to actually program my microcontroller remotely. For this I'm sending all the HEX code through a Gprs modem that is connected to my controller through UART. Now how can I make the controller understand that what ever i'm sending is the hex code and it is supposed to store in an array.

For instance, if i'm sending FE as a single value. The UART sends F first and E next & i.e in ascii format.
I want the controller to take these two characters and show it as a single value as "0xFE" in hex.
 

You have to convert the characters to binary and then combine them again.

1. Convert 'F' (=0x46 in ascii) to 0x0F and store it as 'highbyte'. Do this by subrtracting 0x30 and if the result is still > 9, subtract 7.
2. Do the same for the second byte so 'E' becomes 0x0E.
3. Shift the 'highbyte' to the left 4 times and add the second byte to it to get the result..

Brian.
 

If you check the ASCII table you'll see that the ASCII values for chars A..H and 0..9 are :


Code:
ASCII    DEC
'0'            48
'1'            49
'2'            50
'3'            51
'4'            52
'5'            53
'6'            54
'7'            55
'8'            56
'9'            57
'A'            65
'B'            66
'C'            67
'D'            68
'E'            69
'F'            70



assume that you send chars '9' and 'D' for 0x9D.
What you need to do is to subtract 48 from '9' in order to convert it to 9 and subtract 55 from D in order to convert it to a 13 (which is hex D).
Then you need to combine these two nibbles to a byte using the shift operator so that one goes to the upper nibble 0x00 and the other one to the lower nibble 0x00

In order to do all these you can use a condition, if the received value is between 48 and 57 then it is '0'..'9' so you should subtract 48.
If the received value is between 65 and 70 then it is 'A'..'F' so you should subtract 55.
Then combine these two to a byte.

If you also want to be able to receive lower case 'a'..'f' then you should check for that range of ASCII values too.

Alex

p.s. The nibble definition is four bits https://en.wikipedia.org/wiki/Nibble
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top