Converting ascii hex values to hex bytes

Paul John Falstad pfalstad at phoenix.Princeton.EDU
Fri Oct 19 13:07:13 AEST 1990


In article <1990Oct18.144906.5278 at hellgate.utah.edu> msmith%peruvian.utah.edu at cs.utah.edu (Matthew Smith) writes:
>does a printf %x, he'll get the decimal number in hex format, if he prints it
>with a %c, he'll get the character with that ascii value.  So, all he has to
>do is read in the numbers into chars.  If he is reading in the numbers as a 
>string, ie: '1','2','2', all he has to do is do a atoi, which will return 
>122 in a 16 bit integer, and then cast it into a char, and he'll have 122 in 
>1 char.  ie:  The 1 character ascii value of a number.  

To be picky, the cast is unnecessary, since chars are converted to ints
anyway when used as function arguments.  The following code works:

printf("%c%c\n",65,'a');

to print two a's.  I suggest putchar in this case, however.

--
Paul Falstad, pfalstad at phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
And Dinsdale said, "You've been a naughty boy, Clement," and splits me nostrils
open, and saws me leg off, and pulls me liver out.  And I said, "My name's not
Clement."  And then he loses his temper.  And he nails me head to the floor.



More information about the Comp.lang.c mailing list