Thread: convert integer in little_endian to big_endian

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    convert integer in little_endian to big_endian

    write a program to convert an integer in little-endian format to big-endian...
    please reply ASAp

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Determine the size of an integer using sizeof(int) so you know how many bits to shift, and then use right shift and AND operations to get the individual bytes. Then just add them upp to get the reversed integer.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <arpa/inet.h>
    
    int main()
    {
        int littleEndian = 5;
        int bigEndian = htonl( littleEndian );
        return 0;
    }
    Done.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    It's probably for a homework assignment, so I doubt they're allowed to use POSIX functions.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Memloop View Post
    It's probably for a homework assignment, so I doubt they're allowed to use POSIX functions.
    I know, that's why I did it that way.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  3. Convert integer to char..
    By sniper83 in forum C Programming
    Replies: 7
    Last Post: 07-18-2007, 01:23 PM
  4. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM