printf

S

Shane

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. If I cast the pointer to (char *) then the data is printed out in the
UniCode(?) equivalent of the data.
If I try to cast to (int *) then I only get the address of the pointer (I
think). What I really want is all the ones and zeros to be printed out.

Any help appreciated
TIA
 
B

Barry Schwarz

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. If I cast the pointer to (char *) then the data is printed out in the
UniCode(?) equivalent of the data.
If I try to cast to (int *) then I only get the address of the pointer (I
think). What I really want is all the ones and zeros to be printed out.

What is the exact type of the pointer? "Binary data" is term that has
had several different meanings in various posts. What does the
pointer really point to? Give a short example if possible.


Remove del for email
 
S

Shane

Barry said:
What is the exact type of the pointer?
Void

"Binary data" is term that has
had several different meanings in various posts. What does the
pointer really point to?

Binary data, 1's and 0's
I can interpret the data, if I can read it.
Give a short example if possible.

1111 1010 1100 1110
 
D

dan

Binary data, 1's and 0's
I can interpret the data, if I can read it.


1111 1010 1100 1110

By example, I think Barry means an example program that can be
compiled. Otherwise, nobody can understand what you're saying exactly.

Daniel Goldman
 
S

Shane

dan said:
By example, I think Barry means an example program that can be
compiled. Otherwise, nobody can understand what you're saying exactly.

Daniel Goldman

Ah, my mistake.

Unfortunately I dont think any code I provide will clear things up further
(The pointers are returned from librarys that arent, in my limited
experince, well documented.)

Suffice to say I have tried
printf((char *)pointer_to_bin_data);
and
printf((int *)pointer_to_bin_data);

I appreciate this is far less than what you are asking for, but is all I
able to provide at this point.
 
I

Ian Collins

Shane said:
Binary data, 1's and 0's
I can interpret the data, if I can read it.


1111 1010 1100 1110
That isn't a lot of help, is the data textual (does it look like the
above in an editor), or raw data?

If it is the former, you can probably just cast to char* and print it,
otherwise you have to know the exact format of the data in oder to
convert it into something meaningful.
 
S

Shane

Ian said:
That isn't a lot of help, is the data textual (does it look like the
above in an editor), or raw data?

If it is the former, you can probably just cast to char* and print it,
otherwise you have to know the exact format of the data in oder to
convert it into something meaningful.

Hi
from my OP
Which isnt what I want.

As far as I can tell the exact format of the data is bits, which, once I can
see them, I am going to convert to something meaningful.
 
I

Ian Collins

Shane said:
As far as I can tell the exact format of the data is bits, which, once I can
see them, I am going to convert to something meaningful.
Unless you know what the format is, how can you convert the data?
 
P

Pietro Cerutti

Shane said:
Binary data, 1's and 0's
I can interpret the data, if I can read it.


1111 1010 1100 1110

If you know the length of the array, you may want to print each element
(byte) of the array as an hex value.
 
S

Shane

Ian said:
Unless you know what the format is, how can you convert the data?

Im missing something in the translation.

The data is binary. Its a dump of a packet as received at eth0
Once I can see the ones and zeros I can sort out the structure.
 
M

Mohan

Ah, my mistake.

Unfortunately I dont think any code I provide will clear things up further
(The pointers are returned from librarys that arent, in my limited
experince, well documented.)

Suffice to say I have tried
printf((char *)pointer_to_bin_data);
and
printf((int *)pointer_to_bin_data);

assuming you can dump data in hex:

void xprint_binary_data (void *pointer_to_bin_data, int len)
{
unsigned int *t = (unsigned int *)pointer_to_bin_data;
int i, ui_len = (len / sizeof (unsigned int));

for (i = 0; i < ui_len; i++)
{
printf ("%8.8x ", t);
if (i % 8 == 0) printf ("\n");
}
printf ("\n");
}


Mohan
 
S

Shane

Pietro said:
If you know the length of the array, you may want to print each element
(byte) of the array as an hex value.

Awesome, thats what I can do.
How do I find the length of the array?
 
I

Ian Collins

Shane said:
Im missing something in the translation.

The data is binary. Its a dump of a packet as received at eth0
Once I can see the ones and zeros I can sort out the structure.
Then as other have suggested, dump it as hex. Or better still, use a
decent packet snooper to log and dump the data.
 
K

Keith Thompson

Shane said:
Suffice to say I have tried
printf((char *)pointer_to_bin_data);
and
printf((int *)pointer_to_bin_data);

I *hope* that didn't work.

The first argument to printf is a format string (actually a pointer to
a format string). The first call at least passes soething of the
right type, but it's probably going to print whatever
pointer_to_bin_data points to on stdout as raw binary data, up to the
first '\0' character that it happens to see -- but if it happens to
run across a '%' character, it's likely to interpret it as a directive
and try to consume more arguments. Kaboom.

The second call doesn't even pass the correct type. Your compiler
should complain about it; if it doesn't, you probably forgot the
required '#include <stdio.h>'.
 
R

Richard Heathfield

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
 
P

Pietro Cerutti

Shane said:
Awesome, thats what I can do.
How do I find the length of the array?

You can't. You have to keep track of it since the beginning (i.e. while
storing data in it).
 
B

Barry Schwarz

Binary data, 1's and 0's
I can interpret the data, if I can read it.


1111 1010 1100 1110

What is this?

Is it the 19 characters in a string?

Is it the binary representation of a 16 bit short? If so, is
it little- or big-endian? If it is little-endian, is your data in
memory order (so the value is 0xcefa) or does your data represent the
value of the object (in this case 0xface)?

Is it a 16-bit unicode character?

We know binary data consist of ones and zeros. But the words one and
zero have different meanings in different contexts. Give us the
details of your data. What **exactly** does your void* point to?


Remove del for email
 
A

Army1987

What is this?

Is it the 19 characters in a string?

Is it the binary representation of a 16 bit short? If so, is
it little- or big-endian? If it is little-endian, is your data in
memory order (so the value is 0xcefa) or does your data represent the
value of the object (in this case 0xface)?

Is it a 16-bit unicode character?

We know binary data consist of ones and zeros. But the words one and
zero have different meanings in different contexts. Give us the
details of your data. What **exactly** does your void* point to?
I think he just wants a raw representation of bits, if I
understand him correctly. That is, *(unsigned char *)ptr is 0xFA
and *((unsigned char *)ptr + 1) is 0xCE (assuming CHAR_BIT is 8),
or *(unsigned char *)ptr is 0xFACE (assuming CHAR_BIT is 16). How
many bytes he wants to print, that's another matter.
 
S

Shane

Ian said:
Then as other have suggested, dump it as hex. Or better still, use a
decent packet snooper to log and dump the data.

Hmm, well bright spark, how do you think those packet sniffers were written?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top