Thread: Binary Output

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Binary Output

    Hi there, i currently have strings with hex values in,

    is there a way i can print to see the binary value (im playing around trying to shift bits left and right)

    such as

    %x prints hex
    %c char etc is there one for binary or what is the easiest way to do it?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Far as I know there's no %binary function in printf()... but it's pretty easy to fake with about 5 or 6 lines of code.

    You know the underlying storage of integers in all number bases is binary, so you're already half way there.
    What you need to do is stop and think about the problem... how can you look at only 1 bit of a binary number?

    Honest it's not hard...

    If you get stuck, post your code and we'll see what we can accomplish.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by r_james14 View Post
    Hi there, i currently have strings with hex values in,

    is there a way i can print to see the binary value (im playing around trying to shift bits left and right)

    such as

    %x prints hex
    %c char etc is there one for binary or what is the easiest way to do it?
    To print a value in binary you could use itoa and specify the base. You could also create your own function, if you want to obtain the result as a long long or a long.

    You may find this tutorial useful:
    Dystopian Code: Radix Conversion in C

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    This is related to what I'm trying to achieve but don't really fit under the thread header.

    I am currently writing code for CanOpen which uses 8 predefined bytes. Each byte has a different role. It also use little endian.

    If i put hex value 3 into a byte (2 bits, 11) is there a way i can make sure the code recognises the fact that the value should be 00000011. Or as it is little endian should it be 11000000 in which case i can just shift bits to achieve it?
    thanks

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by r_james14 View Post
    is there a way i can print to see the binary value (im playing around trying to shift bits left and right)
    Bitshift right one at a time and use logical & with 1.

    Quote Originally Posted by r_james14 View Post
    Or as it is little endian should it be 11000000 in which case i can just shift bits to achieve it?
    thanks
    Altho the system may be little endian, WRT to bitshifting, etc, they function as big endian (ie, endianess is irrelevant except in cases where data is from an external source, in which case the system cannot tell the difference. I don't know what "CanOpen" is so I am not sure how that applies).
    Last edited by MK27; 11-21-2011 at 06:16 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    BTW, that method only works with unsigned types (or positive numbers)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    okay so since my last annoying questions ive done alot more reading and since realised that endian big/little refers to byte order and not bit order.

    So if i have a hex value ABCD which i need to split over two bytes.

    SendData [2];
    SendData [3];

    What is the best way to break the string up so SendData[2] = CD and SendData[3] =AB.

    Im not asking for anyone to do it for me, just is the already a feature to do this is a really simple way that i can't think of atm.

    Thanks

    James

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by r_james14 View Post
    okay so since my last annoying questions ive done alot more reading and since realised that endian big/little refers to byte order and not bit order.
    Yes, but FYI what I said about bitshifting still applies. If you >>16 a 32-bit int, you get the same result regardless of the endianess of the system. The C standard requires this, I believe.

    What is the best way to break the string up so SendData[2] = CD and SendData[3] =AB.
    You could use a union. Nb, that endianess does appear there. Eg. on a little endian system:

    Code:
    union number {
    	short int x;
    	char bytes[2];
    };
       
    	union number n;
    	n.x = 0xabcd;
    	printf("%hhx %hhx\n", n.bytes[0], n.bytes[1]);
    You'll get "cd ab".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    Im sure im going to annoy everyone today with amount of posts.

    Ive taken the suggests from previous post and had a go at implementing it. Code is below.

    Problem is if i assign n.x = 0xABCD; in the union in my function the output is perfect. However is i change it to n.x = Index then it only picks up the CD not the AB. My code is put below.


    Code:
    #include <stdio.h>
    
    
    char SendData[8];
    static void MakingSDOmessage(char Index, char SubIndex, int Value);
    int i;
    
    
    main()
    {
    
    
    MakingSDOmessage(0xABCD, 0xe3, 0);
    
    
    for ( i=0; i < 8; i++)
      printf("SendData[%d] = 0x%x\n", i, SendData[i]);
    
    
    
    
    
    
    while(1)
    {
    }
    }
    
    
    static void MakingSDOmessage(char Index, char SubIndex, int Value)
    {
    union number {
        short int x;
        char bytes[2];
    };
        
        union number n;
        n.x = 0xABCD;
        SendData[1] = n.bytes[0];
        SendData[2] = n.bytes[1];
        SendData[3] = SubIndex;
    
    
    }

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    solved it, it was because i had to use wchar_t to define the char Index

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    should the following code work?

    i keep getting a n: hasnt been initialized warning and some strange results for the SendData[4] -- [6]

    Code:
    #include <stdio.h>
    
    
    #define Output 0xABCD
     
    char SendData[8];
    static void MakingSDOmessage(wchar_t Index, char SubIndex, wchar_t Value);
    int i;
     
     
    main()
    {
    
    
    MakingSDOmessage(Output, 0xa9, 0x17A120);
     
    for ( i=0; i < 8; i++)
      printf("SendData[%d] = 0x%x\n", i, SendData[i]);
      
    while(1)
    {
    }
    }
     
    static void MakingSDOmessage(wchar_t Index, char SubIndex, wchar_t Value)
    {
    	union index {
    		short int x;
    		char bytes[3];
    };
    
    
        union index n;
        
    	n.x = Index;
    	SendData[0] = 0Xe2;
        SendData[1] = n.bytes[0];
        SendData[2] = n.bytes[1];
        SendData[3] = SubIndex;
    
    
        n.x = Value;
    	SendData[4] = n.bytes[0];
        SendData[5] = n.bytes[1];
    	SendData[6] = n.bytes[2];
    }

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why are you using a wchar_t? A wchar_t does not have a standard size -- eg, on my system it is 4 bytes, but apparently some compilers may (bizarrely) use only 1.

    Wide character - Wikipedia, the free encyclopedia

    For consistency, IMO you should be using a short int, or a 2 byte char array.

    Also, your union is screwed up. A short int is 2 bytes, but "bytes" is 3.

    Actually, short ints don't have a guaranteed size either. The best practice here is either the char array or:

    Code:
    #include <inttypes.h>
    uint16_t (or int16_t)
    And use that in place of the shorts and wchars. 16 bits is always 16 bits.

    BTW I kind of thing you might be unnecessarily complicating the whole task. Why don't you just create a struct to hold everything by type instead of using the SendData array?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    hi there, i tried the uint16_t and it just will not compile on visual studio 2008 also my "value" entry is 32 bit. As regards putting the data into an array or structure i'll still have the fundamental issues i have of splitting up the values up.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    However is i change it to n.x = Index then it only picks up the CD not the AB.
    Because a character only holds a limited number of bits. In your case, it's 8 which can only hold 0xCD, not 0xABCD which is 16 (at least) bits.

    VS2008 doesn't pretend to be C99 compliant so it doesn't have the uint*_t types. Use unsigned int instead of wchar_t. And get rid of the nonsense about unions and make the code portable :

    Code:
    unsigned char SendData[8];
    
    static void MakingSDOmessage(unsigned int Index, unsigned char SubIndex, unsigned int Value)
    {
        n.x = Index;
        SendData[0] = 0xE2;
        SendData[1] = index;
        SendData[2] = index >> 8;
        SendData[3] = SubIndex;
     
        SendData[4] = value;
        SendData[5] = value >> 8;
        SendData[6] = value >> 16;
    }
    Last edited by KCfromNC; 11-22-2011 at 09:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encrypting messages in the binary and the output???
    By razor in forum C Programming
    Replies: 5
    Last Post: 06-09-2011, 11:50 AM
  2. binary based output
    By mk555 in forum C Programming
    Replies: 15
    Last Post: 09-24-2004, 06:27 PM
  3. Binary Output
    By Ajsan in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2004, 04:43 PM
  4. displaying binary output
    By clancyPC in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 07:32 AM
  5. Specifying binary output?
    By Hardboy in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2003, 03:41 PM