[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] C help plz..



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 [matthewd@research.canon.com.au] wrote:
> 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;
> 	unsigned char c;
> 	:
> 	:
> 	c = (l & 0xFF000000) >> 24;
> 	putchar(c);
> 	c = (l & 0x00FF0000) >> 16;
> 	putchar(c);
> 	c = (l & 0x0000FF00) >> 8;
> 	putchar(c);
> 	c = l & 0x000000FF;
> 	putchar(c);
> 
> Is this what you were looking for? I suppose you wanted something
> simple, ie using printf... I'm not sure about that.
> 
> 	printf
> 	(
> 		"%c%c%c%c",
> 		(l & 0xFF000000) >> 24,
> 		(l & 0x00FF0000) >> 16,
> 		(l & 0x0000FF00) >> 8,
> 		l & 0x000000FF
> 	);
> this might be as close as you can get.
> 
> Shao Zhang wrote:
> > 
> > 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 [matthewd@research.canon.com.au] wrote:
> > > Try using %X or %x instead of %ld
> > >
> > > 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.
> > > >
> > > >         Thanks.
> > > > Shao.

-- 
____________________________________________________________________________
Shao Zhang - Running Debian 2.1  ___ _               _____
Department of Communications    / __| |_  __ _ ___  |_  / |_  __ _ _ _  __ _ 
University of New South Wales   \__ \ ' \/ _` / _ \  / /| ' \/ _` | ' \/ _` |
Sydney, Australia               |___/_||_\__,_\___/ /___|_||_\__,_|_||_\__, |
Email: shao@cia.com.au                                                  |___/ 
_____________________________________________________________________________


Reply to: