Peter said:
CBFalconer was jivin' on Thu, 16 Oct 2003 06:07:04 GMT
Hardly. You are making huge assumptions about the sizes of long and
unsigned char. Who says sizeof(long) is 8? And who says a byte is 8
bits? These assumptions are evident in your code, and are decidedly
non-portable.
But it is, if you say the object is to emit 8 pairs of hex digits,
with the first representing the least significant bits. This
gives a fixed output field. Conceded, it truncates anything past
64 bits in an unsigned long.
If the objective is to dump all digits, and ignore the more
significant zeroes, you can simply exit the loop when the value
becomes zero.
In both cases the technique is independant of CHAR_BIT and
sizeof(long) and, most important, independent of endianess.
We could detect the bits needed by a combination of CHAR_BIT and
sizeof(long), possibly also examining ULONG_MAX, but the result
would no longer be a fixed field. The question is: What is
wanted.