[En-Nut-Discussion] ASCII to hex
Jesper Hansen
jesperh at telia.com
Mon Mar 1 18:14:40 CET 2004
As I assume the HEX value is to be printed (otherwise
it wouldn't make much sense), here's another classic:
void phex(u_char c)
{
u_char *s = "0123456789abcdef";
putchar(s[c>>4]);
putchar(s[c&15]);
}
or maybe :
void phex(u_char c)
{
putchar((c>>4) > 9 ? (c>>4)+'W' : (c>>4)+'0');
putchar((c&15) > 9 ? (c&15)+'W' : (c&15)+'0');
}
/Jesper
www.yampp.com
----- Original Message -----
From: "Alexander Baranov" <Baranov at intech21.com>
To: "Ethernut User Chat (English)" <en-nut-discussion at egnite.de>
Sent: Monday, March 01, 2004 5:45 PM
Subject: Re: [En-Nut-Discussion] ASCII to hex
> Hi
> maybe this helps until seniors propose something like atoi()?
>
> unsigned char pack_char_to_hex (unsigned char* psrc)
> {
> unsigned char m0,m1;
>
> m0 = *psrc;
> if((m0 >=0x30)&&(m0<=0x39)){
> m0 = m0 - 0x30;
> }
> else if((m0 >=0x41)&&(m0<=0x46)){
> m0 = m0 - 0x41+0x0A;
> }
> else if((m0 >=0x61)&&(m0<=0x66)){
> m0 = m0 - 0x61+0x0A;
> }
> else{
> m0 = 0;
> }
>
> m1 = *(psrc + 1);
> if((m1 >=0x30)&&(m1<=0x39)){
> m1 = m1 - 0x30;
> }
> else if((m1 >=0x41)&&(m1<=0x46)){
> m1 = m1 - 0x41+0x0A;
> }
> else if((m1 >=0x61)&&(m1<=0x66)){
> m1 = m1 - 0x61+0x0A;
> }
> else{
> m1 = 0;
> }
>
> return ( (m0<<4)+m1);
> }
> ----- Original Message -----
> From: "Sigurd Kleppan" <sigurkl at frisurf.no>
> To: <en-nut-discussion at egnite.de>
> Sent: Monday, March 01, 2004 10:58 AM
> Subject: [En-Nut-Discussion] ASCII to hex
>
>
> > A simple question?
> > I need to convert a ASCII character to it's hex value. Do someone have a
> function or a hint.
> >
> > Thank you!
> >
> > Sigurd Kleppan
> > sigurkl at frisurf.no
> >
> > _______________________________________________
> > En-Nut-Discussion mailing list
> > En-Nut-Discussion at egnite.de
> > http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
>
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
More information about the En-Nut-Discussion
mailing list