Look at his code again. It does not take a byte value in the range 0-255 and
turn it into a (u)short in the range 0-255. It takes each bit position
within the source byte and maps it into 2 adjacent bit positions in the
resulting (u)short. A cast cannot replicate this behavior.
To the original poster: I would suggest to you that when processing image
data, one "standard" way of doing this is simply to replicate the byte into
both byte positions of the word. If you want to make sure that you only use
the positive range of a signed 16 bit value, you can then shift right by one
bit. Or if you want to use the full range of a signed 16 bit value, you can
xor by 0x8000.
private static short ByteToShort(byte b)
{
return (short)(((b << 8) | b) ^ 0x8000); // use full range of signed
16 bit value
}
If you really want to stick with the mathematical outcome of the method
you've got now, I would suggest using a simple lookup table of 256
(u)shorts.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl...
private static short ByteToShort(byte b)
Why don't you just assign the byte to the short or int? Both short and
int can hold any values that a byte can [0-255].
if( b < 0 )
return (short) ( word - 32768 );
Since a byte is unsigned, that will never happen. If you want to treat
the byte as signed, use sbyte instead.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.