One could easily expand the code snippet to something more like... int i; char c; unsigned long l; : : for (i = sizeof(unsigned long); i > 0; i--) { int shift_factor = (i - 1) * 8; c = (char)((l & ((unsigned long)0xFF << shift_factor)) >> shift_factor); putchar(c); }
... if you want to be pedantic about the size of an unsigned long. Sean 'Shaleh' Perry wrote: > > On 16-Mar-2000 Shao Zhang wrote: > > Thanks. This is exactly what I want. I have thought about doing it this > > way, it is just that from memory, there is a libc function that does the > > equivalent. > > > > What was given is the only safe and sane way I have ever seen. Bigger > question > is why do you have a unsigned long (which can be upwards of 8 bytes (64bits)) > output only 4bytes (32bits)? remember, not every machine uses int === long.