Re: printf
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 31 Jul 2007 08:27:44 +0000
Shane said:
Hi all, I am learning C, and I have a problem with printf.
I have a pointer that points to binary data, and I want to print that
data
out.
#include <stdio.h>
#include <limits.h>
void binprint(FILE *fp, void *vp, size_t n)
{
unsigned char *p = vp;
unsigned char mask = 0;
while(n--)
{
mask = 1 << (CHAR_BIT - 1);
while(mask > 0)
{
putc('0' + ((*p & mask) != 0), fp);
mask >>= 1;
}
++p;
}
}
/* driver */
#include <time.h>
int main(void)
{
int i = 42;
double d = 3.1415926;
time_t tt = time(NULL);
struct tm *t = localtime(&tt);
printf("int : "); binprint(stdout, &i, sizeof i); putchar('\n');
printf("double : "); binprint(stdout, &d, sizeof d); putchar('\n');
printf("struct tm: "); binprint(stdout, t, sizeof *t); putchar('\n');
return 0;
}
Sample output:
int : 00101010000000000000000000000000
double : 0100101011011000000100100100110111111011001000010000100101000000
struct tm: 0000101000000000000000000000000000011011000000000000000000000000000010000000000000000000000000000001111100000000000000000000000000000110000000000000000000000000011010110000000000000000000000000000001000000000000000000000000011010011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010101000100111110000010000001000
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
.
- References:
- printf
- From: Shane
- printf
- Prev by Date: Re: circular shift array
- Next by Date: Re: circular shift array
- Previous by thread: Re: printf
- Next by thread: circular shift array
- Index(es):
Relevant Pages
|