unpacking after an "or" and a "shift"
hi everyone.
right now i have read in two characters and combined them into an int. Once i have combined them, i have shifted the whole thing over.
Now i need to take the combined shift and unpack it to two separate entities again. Here is my code so far.
while(!fIn.eof()) {
fIn.get(Cha);
fIn.get(Chb);
combine = ((int)Cha<<8) | (int)Chb;
shift = (combine << 4);
}
Is what i'm looking to do remotely possible? If so, can anybody help me out? Thanks much!
Keith
Re: unpacking after an "or" and a "shift"
Quote:
Originally posted by keithmolo
fIn.get(Cha);
fIn.get(Chb);
combine = ((int)Cha<<8) | (int)Chb;
shift = (combine << 4);
all you have to do is reverse the steps.
combine = shift >> 4;
Cha = combine >> 8;
Chb = combine & 0xFF;
done
Re: Re: unpacking after an "or" and a "shift"
Quote:
Originally posted by FillYourBrain
Cha = combine >> 8;
Chb = combine & 8;
could you please explain how this works? I understand the unpacking part, but am unfamiliar with what values the chars would be taking on. Also, what does the & do in this particular case?
thanks,
keith
not working with some numbers
for testing purposes of my code, i created a dat file with the following hex code:
------------------------------------------------------------------
01 02 03 04 05 06 07 08 :: F8 F9 FA FB FC FD FE FF
------------------------------------------------------------------
given my code, and, provided, this is in a loop:
Code:
fIn2.get(xCha);
fIn2.get(xChb);
(unsigned int)combine = ((unsigned int)xCha<<8)|(unsigned int xChb;
(unsigned int)shift = (combine << 4);
xCha = (shift >> 8);
xChb = (shift & 0xFF);
fout.put(xChb);
fout.put(xCha);
i believe it should write hex code that looks like so:
-------------------------------------------------------------
20 10 40 30 60 50 80 70 :: 90 8F.....and so on
-------------------------------------------------------------
but the code seems to have trouble when it hits the negative hex numbers, because it looks like this:
Code:
20 10 40 30 60 50 80 70 :: 90 FF B0 FF D0 FF F0 FF
In fact, it writes FF's for every other character across the board. I am stumped!
Any ideas?
Thanks,
Keith