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 t
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
You could just do:
fflush (stdout); /* clear the stream buffer */
write (1, myvar, 4); /* write binary to stdout */
jim
> 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.
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.
shao.
Matthew Dalton [EMAIL PROTECTED] wrote:
> Sorry... I automatically made a link between binary data and hexadecimal
> data...
>
> You
Sorry... I automatically made a link between binary data and hexadecimal
data...
You could shift 8 bits of the unsigned long into a unsigned char one at
a time, and print that character with a %c in the printf, or use
putchar() or something.
eg:
unsigned long l = 0x38c9616e;
unsig
On Thu, Mar 16, 2000 at 03:04:53PM +1100, Shao Zhang wrote:
> Hi,
> If I have an unsigned long int, instead printing out its values
> in string using printf("%ld\n", my_var),
>
> I would like to print it out as a 4-byte binary data. Is there
> any easy way to do this in C.
But isn't %[Xx] just prints out as Hexdecimal?
I just tried, and it prints out something like: 38c9616e
which consumes 8 bytes in a file. Given that unsigned long is 32 bits,
I want to use exactly 4 byte to represent it in order to save some
space.
Thanks.
Shao.
Matthew Dalton [EMAIL PROTECTED]
Hi,
If I have an unsigned long int, instead printing out its values
in string using printf("%ld\n", my_var),
I would like to print it out as a 4-byte binary data. Is there
any easy way to do this in C.
Thanks.
Shao.
--
8 matches
Mail list logo