# endian problem

• 07-31-2003
keithmolo
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
• 07-31-2003
funkydude9
You would have to use arrays to flip-flop every two numbers, like:

while (eof)
{
-switch them in the array
}
- write to output file
• 07-31-2003
keithmolo
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
• 07-31-2003
swoopy
Here's one way.
Code:

```  while(!fIn2.eof()) {       while (fIn2.get(xCh)){         if (fIn2.get(yCh))             fout.put(yCh);         fout.put(xCh);       }   }```
• 07-31-2003
FillYourBrain
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);```
• 08-01-2003
Cat
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) ); }```
• 08-01-2003
swoopy
Hmm, you're right Cat, he does say bits.