Converting Hex values to ascii values in C

Status
Not open for further replies.

Davechika

Newbie level 6
Joined
Oct 8, 2009
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Port-Harcourt,Nigeria
Activity points
1,357
How can can i convert hex values e.g (0x40,0x12,0x0A)retrived from 89S52 counters to ascii decimal values e.g(64,18,10)which is sent to the serial buffer (SBUF) of 8952 microcontroller.
 

How about?

char *hex="0x40";
int hex2dec(char *h)
{
int out=0;
if((h[2]>='a' && h[2]<='f')
{
out=h[2]-'a'+10;
}
else if((h[2]>='A' && h[2]<='F')
{
out=h[2]-'A'+10;
}
else
{
out=h[2]-'0'+10;
}

if((h[1]>='a' && h[1]<='f')
{
out=out*16+h[1]-'a'+10;
}
else if((h[1]>='A' && h[1]<='F')
{
out=out*16+h[1]-'A'+10;
}
else
{
out=out*16+h[1]-'0'+10;
}

}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…