Converting ascii hex values to hex bytes

brian_helterline brianh at hpcvia.CV.HP.COM
Fri Oct 19 02:44:13 AEST 1990


Matthew Smith writes:
[(Mark Pledger) stuff deleted]

>Well, I'm not sure I understand you correctly, because if I do, then things
>will be fairly simple.  In C, there is no difference in the way a char and an
>integer are represented, except that a char only has 4 bits as opposed to 8.
                                        ^^^^^^^^^^^^^^^^^^^^
	WHAT?????? a char has 8 bits!!  4 bits yield values from 0-15.
    
>As a matter of fact, chars are only integers that range from 0 to 255.  Maybe
>the following piece of code will help clarify things:


>char a;

>a=64;
>printf("%c, %x\n",a,a);

>you'll get an output of "a" and "40" repectively.  "a" being the ascii 
>value of 64, and "40" being the hex value of 64.

>Using this idea, you should just read all of the numbers into char variables
>and then print them out in hex.

	This is *exactly* what the original poster did NOT want!  He did
	not want the hex value converted to 2 chars and output as a string,
	he wanted to output 1 char whose ascii value was the hex value
	he wanted.  (In this discussion, hex really isn't important, since
	it it just the integer value and it can be thought of in hex, octal
	binary, etc )

>I hope this helps...

>(I'm sure this is going to bring on a huge discussion on how having a "char"
>variable is NOT the same thing as an integer between 0 and 255, and how K&R 
>says this, and Joe says that, and the other thing.  So, please forgive me
>for making such a statement and let's not bog down the net with such trivial
>details)
	
	Calling a char 4 bits is not trival.

>Matt Smith
>----------



More information about the Comp.lang.c mailing list