This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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....

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

    or

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

    Jon

  • Thanks Jhon.....It works..


    But how can we send Binary Data when we have a float data type instead of integer.

  • "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!

  • "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!

  • 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