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

T. William Wells bill at twwells.com
Sun Jan 7 18:02:39 AEST 1990


From: bill at twwells.com (T. William Wells)

In article <498 at longway.TIC.COM> uunet!stealth.acf.nyu.edu!brnstnd (Dan Bernstein) writes:
: From: brnstnd at stealth.acf.nyu.edu
:
: The question is self-explanatory. This is a practical question as well
: as a theoretical one: I'd like a solution that is both conformant and
: portable in the real world. Does (int) (unsigned int) ch do the trick?
: What about (int) (unsigned char)?

Excuse, but this is purely a C question, so I've directed
followups to comp.lang.c.

[ I'm not sure I agree, but I don't see comp.std.unix people clamoring
to answer this question, so let's send it to comp.lang.c to see if there
is more interest there.  -mod ]

 Anyway, I don't think that either is
guaranteed. One that is, assuming that the character is not in a
register, is: *(unsigned char *)&ch.

(NB: a char might be converted to a number larger than 255 if
characters are larger than eight bits.)

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com

Volume-Number: Volume 18, Number 15



More information about the Comp.std.unix mailing list