How to convert a char into an int from 0 through 255?

Walter Bright bright at Data-IO.COM
Tue Jan 9 06:43:06 AEST 1990


From: bright at Data-IO.COM (Walter Bright)

In article <498 at longway.TIC.COM> uunet!stealth.acf.nyu.edu!brnstnd (Dan Bernstein) writes:
<I'd like a solution that is both conformant and
<portable in the real world.
<Does (int) (unsigned int) ch do the trick?
No.

<What about (int) (unsigned char)?
Yes, assuming chars are 8 bits. But be warned, some older K&R compilers I've
run across do not do that cast correctly, even though the expression is correct.

I prefer using:
	char c;
	int i;
	i = c & 0xFF;	/* assuming chars >= 8 bits	*/

P.S. I don't care about 1's complement machines.

Volume-Number: Volume 18, Number 12



More information about the Comp.std.unix mailing list