469,648 Members | 1,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Scaling byte to short


I'm looking for the alogithm to take a piece of 8-bit audio data, and
scale it to a 16-bit short. I got as far as:

private static short ByteToShort(byte b)
{
int word = 0;
word |= ( ( b & 0x01 ) << 1 );
word |= ( ( b & 0x02 ) << 2 );
word |= ( ( b & 0x04 ) << 3 );
word |= ( ( b & 0x08 ) << 4 );
word |= ( ( b & 0x10 ) << 5 );
word |= ( ( b & 0x20 ) << 6 );
word |= ( ( b & 0x40 ) << 7 );
word |= ( ( b & 0x80 ) << 8 );
if( b < 0 )
return (short) ( word - 32768 );
else
return (short) ( word );
}

(The "word" was originally a "short", but I kept getting CS0675).

The wall has got a hole in about the size of my head, so your help
would be appreciated!

Rgds,
Nov 15 '05 #1
5 2380
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.
Nov 15 '05 #2
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.

Nov 15 '05 #3
On Wed, 26 Nov 2003 22:52:00 +0100, Mattias Sjögren
<ma********************@mvps.org> wrote:
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.


Yeah, I realised that while debugging - changing to sbyte didn't solve
it.

I couldn't simply cast as it's audio data I'm playing with (I needed
to preserve the waveform). Anyway, I've just sovled it. That's the
last time I do any bit arithmetic... ;)

Thanks,

Andy


Nov 15 '05 #4
On Wed, 26 Nov 2003 14:44:20 -0800, "Ted Miller" <te*@nwlink.com>
wrote:
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.


Many thanks Ted, I arrived at pretty much the same formula! I could
have done with you about 4 hours ago! :)

Rgds,

Nov 15 '05 #5
Look at his code again.


Oops, you're right, my bad.

But at least I was right on the fact that a byte can't be < 0. :-)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Bryan Parkoff | last post: by
4 posts views Thread by TRW1313 | last post: by
9 posts views Thread by Alberto Cardoso | last post: by
11 posts views Thread by K Viltersten | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.