472,378 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 2515
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
4
by: TRW1313 | last post by:
I'm looking to populate a byte array of some fixed size to send out over a UDP connection. The data in the byte array is mixed between characters and binary. I'm a beginner to this language. ...
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
2
by: Howard Weiss | last post by:
I am reading a file (containing short integers). To read the file, I use the following FileStream *myFile = new FileStream(FileName, FileMode::Open, FileAccess::Read); __int64 myFileSize =...
2
by: aengus | last post by:
Hi, I am looking to find out how to deal with the BYTE datatype in VC, as used below: This struct is part of the API for a Garmin GPS unit: typedef struct { unsigned char mPacketType; ...
9
by: Alberto Cardoso | last post by:
Is there a direct way to convert a short array to a byte array? I dont to use a for and cast every short to a byte. I want something like the BitConverter class that accpets a short array as...
3
by: jackmejia | last post by:
Hello I am fighting to sync a C++ client with a C# server, I have managed to create a byte array in C++ stored as char* to be sent over the network to the server written in C#. on the C# side,...
11
by: K Viltersten | last post by:
While i know that the bytes are cheap today, i still prefer to use a byte (or short) when i know that the entity counted isn't larger than 255 (or 65k). However, it's a real pain to cast every...
10
by: ShadowLocke | last post by:
I'm looking for a better understanding of whats going on with the code i write. I have come across something that I thought I understood but its not working as expected. Its hard to explain so let me...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.