converting bytes to int

This is a discussion on converting bytes to int within the C Programming forums, part of the General Programming Boards category; Hi, I'm getting 4 bytes from a pipe that represent an integer in little endian format. What's the best way ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    converting bytes to int

    Hi,

    I'm getting 4 bytes from a pipe that represent an integer in little endian format.

    What's the best way to convert it to an int?

    TIA, rotis23

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    int my_int = *((int *) byte_pointer);
    Assuming you are using a little endian machine. Otherwise you will have to do some byte swapping.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks, that worked.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    How do I do the reverse and contruct a little endian 4 byte integer for sending through a pipe?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not use these functions at both ends:
    http://www.rt.com/man/htonl.3.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21