Thread: C Program: Convert to Binary and Change Endian-ness

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    C Program: Convert to Binary and Change Endian-ness

    Hello all!

    This is my C program that is supposed to:
    1. prompt user for integer
    2. display integer in decimal
    3. display binary value of integer
    4. flip from little-endian to big-endian and display binary
    5. display NEW big-endian in decimal
    6. quit on user input 'q'.

    First question: this program does not display the binary values properly.. where did I mess up?

    Also:
    Besides alternatives to scanf or printf, or using function calls to convert to binary, where can I improve my code? I'm looking for efficiency, easy implementation etc..

    Thanks!!

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        
        while (1)
        {
            short i;
            short j;
            char input[40];
            
            printf("Enter a 32-bit integer ('q' to quit): ");
            scanf("%s", input);
       
            if (input[0] == 'q')
                return 0;
                
            int number = atoi(input);
    
    
            char little[32];
            
            for( i = 31; i >= 0; i--) 
            {
                if( (1 << i) & number)
                    little[31 - i] = '1';
                else
                    little[31 - i] = '0';                   
            }
        
            char big[32];
            
            for ( j = 4; j > 0; j--)
            {
                for ( i = 8; i > 0; i--)
                {
                    big[(8 * j) - i] = little[(8 * (5 - j)) - i];
                }
            }
            
            int flipped = 0;
            
            for( i = 31; i >= 0; i--) 
            {
                if ( big[i] = '1')
                    flipped = flipped + 2^i;                 
            }
            
            printf("\n32-bit integer: %d\nLittle-endian binary: %s\nBig-endian binary: %s\nNew 32-bit integer: %d\n\n",number,little,big,flipped);
        
        }
        return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So, what are you inputting, what is it outputting, and what were you expecting it to output?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why are i and j declared as short? Unless you are trying to save space (say in a large array) you should always use int, even if the numbers will be small.

    You probably want == instead of = here

    if ( big[i] = '1')


    What do you think this means? Hint, it doesn't mean 2 to the power of i !!!

    2^i

    Use instead what you did earlier:

    (1 << i)


    Overall, though, you seem to be misunderstanding little and big endian. They don't have every bit in reverse order, just the bytes. So for two bytes, if this was big endian

    11110000 00001111

    then this would be little endian

    00001111 11110000
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    input:
    5

    output:
    32-bit integer: 5
    Little-endian binary: 00000000000000000000000000000101ÿÿ
    Big-endian binary:11111111111111111111111111111111000000000000000000 00000000000101ÿÿ
    New 32-bit integer: 64

    EXPECTED output:
    32-bit integer: 5
    Little-endian binary: 00000000000000000000000000000101
    Big-endian binary: 00000101000000000000000000000000
    New 32-bit integer: 327680



  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Thanks for suggestions!

    This line should alternate little/big endians as you described.
    Code:
    for ( j = 4; j > 0; j--) { for ( i = 8; i > 0; i--) { big[(8 * j) - i] = little[(8 * (5 - j)) - i]; } }


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program for testing Endian-ness with requirements
    By Syndicate105 in forum C Programming
    Replies: 1
    Last Post: 02-01-2012, 04:55 PM
  2. Endian change & Byte Array -> Struct
    By bramaen in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 12:55 PM
  3. Change in community-ish-ness
    By Dino in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-27-2008, 05:03 PM
  4. Binary File - Byte order / endian
    By chico1st in forum C Programming
    Replies: 27
    Last Post: 08-22-2007, 07:13 PM
  5. Replies: 13
    Last Post: 10-09-2006, 09:08 AM