-
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
-
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
-
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
-
Here's one way.
Code:
while(!fIn2.eof()) {
while (fIn2.get(xCh)){
if (fIn2.get(yCh))
fout.put(yCh);
fout.put(xCh);
}
}
-
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);
-
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) );
}
-
Hmm, you're right Cat, he does say bits.