Thread: endian problem

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    endian problem

    I am reading my input file one byte at a time and writing each character to my output file, like so:

    while(!fIn2.eof()) {
    while (fIn2.get(xCh)){
    fout.put(xCh);
    }

    The problem resides in the fact that now, i need to write it a little bit differently. If I had bits in this order, for example:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    I would need them written like this:

    2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15
    ----------------------------------------------------------------

    any ideas on how to read in data and write it like this?

    Thanks,
    keith

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    You would have to use arrays to flip-flop every two numbers, like:

    while (eof)
    {
    -read next two numbers
    -switch them in the array
    }
    - write to output file
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    alrighty. say i have to read a lot of bytes (and i do) and switch every two around....what kind of code am i looking at? any short example-code would help a ton.

    thanks,
    keith

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
       while(!fIn2.eof()) {
          while (fIn2.get(xCh)){
             if (fIn2.get(yCh))
                fout.put(yCh);
             fout.put(xCh);
          }
       }

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    //assuming you read 256 bytes into buf
    
    unsigned short *shortbuf = (unsigned short*)buf;
    for(int i=0; i < 128; i++, shortbuf++)
       *shortbuf = (*shortbuf >> 8) | (*shortbuf << 8);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Wait, are you flipping BYTES or BITS around? Your first post seems to say you're flipping bits within each byte.

    In that case:

    Code:
    unsigned char flipBits(unsigned char c){
      return ( ((c & 0x55) << 1) & ((c & 0xAA) >> 1) );
    }
    Last edited by Cat; 08-01-2003 at 10:43 AM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Hmm, you're right Cat, he does say bits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Odd Pointer Problem
    By loopshot in forum C Programming
    Replies: 4
    Last Post: 10-26-2006, 08:49 PM