In article <nvte139ulmkj28f237hh70gh658216dui0@4ax.com>,
Bo Berglund <bo.berglundxxwrote:
Quote:
>So in Delphi I have used the built-in Swap() function for reversing
>the bytes in a 2-byte variable like an "unsigned short int" or
>"word" in Pascal. I also have built a Swap32 function to handle
>4-byte swaps. Now I need to get this to work in C++ and I am
>getting errors all over the place. Help appreciated.
You used a bunch of structs, which is a bit of overkill. C/C++ give
you access to bitshift operators, which allow you to directly pick and
move bits around. Your functions can be rewritten as:
unsigned short Swap16(unsigned short v)
{
return ((v & 0xFF) << 8) |
((v >8) & 0xFF);
}
unsigned long Swap32(unsigned long v)
{
return ((v & 0xFF) << 24) |
(((v >8) & 0xFF) << 16) |
(((v >16) & 0xFF) << 8) |
(((v >24) & 0xFF);
}
Nathan Mates
--
<*Nathan Mates - personal webpage
http://www.visi.com/~nathan/
# Programmer at Pandemic Studios --
http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein