Thread: Reading sbte stream to int

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

    Reading sbte stream to int

    So I have to read in a byte stream from a file, thats easy enough. The problem I'm having is moving the bytes around to create an int. So it goes like this:

    Every four bytes in the file correspond to one integer, but we ignore the first one and move the other 3 around. if the four bytes are: 0x12 0x34 0x56 0x78, then the integer is 0x785634.

    How do I copy the bytes around and create an int from that?

    Many thanks from a non-C guy forced to do some C work.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    unsigned char bytes[4];
    unsigned int word = ((unsigned int)bytes[3] << 16) | ((unsigned int)bytes[2] << 8) | (unsigned int)bytes[1];

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Fantastic

    Many thanks, works perfectly.

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: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM