Discussion Forum

Sending Binary Data with "printf"

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Parus Khan
Posted
5-Sep-2002 21:30 GMT
Toolset
C166
New! Sending Binary Data with "printf"
Hi..


I want to send Binary Data using printf() function

for examle if I do
 printf("%d",i)
where i=16706 then printf() function transmit that integer in text mode (not in Binary mode) i.e. it will send 5 bytes for 16706.
If I use %X instead of %d even then it will send 4142 in HEX....that means 4 bytes.

Now if somhow I can send the number 16706 in binary .... it will become 0xAB which is only two bytes .

so for any integer upto 65636.... I can send it as binary in only two bytes..

I f somone knows...pls let me know.


Thanks....

Read-Only
Author
Jon Ward
Posted
5-Sep-2002 21:43 GMT
Toolset
C166
New! RE: Sending Binary Data with "printf"
putchar(number);
putchar(number>>8);

or

putchar(number>>8);
putchar(number);

Jon
Read-Only
Author
Parus Khan
Posted
5-Sep-2002 22:45 GMT
Toolset
C166
New! RE: Sending Binary Data with "printf"
Thanks Jhon.....It works..


But how can we send Binary Data when we have a float data type instead of integer.
Read-Only
Author
Andrew Neil
Posted
5-Sep-2002 23:25 GMT
Toolset
C166
New! RE: Sending Binary Data with "printf"
"But how can we send Binary Data when we have a float data type instead of integer."

You will have to read the compiler Manual to find the internal representation of the float type, and then devise a means to access the individual bytes of that representation.
This is standard 'C' stuff; the main choices are:
[1] shift & mask - usually portable, but often slow;
[2] use a union - non-portable, but usually quicker

Try a 'Search', as this has definitely been discussed before!
Read-Only
Author
Jon Ward
Posted
5-Sep-2002 23:39 GMT
Toolset
C166
New! RE: Sending Binary Data with "printf"
how can we send Binary Data when we have a float data type

.
.
.
float x;
putchar (((unsigned char *) &x) [0]);
putchar (((unsigned char *) &x) [1]);
putchar (((unsigned char *) &x) [2]);
putchar (((unsigned char *) &x) [3]);
.
.
.

Jon
Read-Only
Author
Andrew Neil
Posted
5-Sep-2002 23:35 GMT
Toolset
C166
New! RE: Sending Binary Data with "printf"
"printf() function transmit that integer in text mode (not in Binary mode) i.e. it will send 5 bytes for 16706"

Well of course it does!
The clue is in the function name: printf - clearly this is aimed at a printing output device, which will naturally expect text output!

This should also serve as a warning to you: it is up to you to ensure that your entire communications link can pass binary data; it only takes one step in the link to interpret your binary data as control codes, and the entire link will get messed up.
Be careful!

Next Thread | Thread List | Previous Thread Start a Thread | Settings