473,657 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(byt e 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 2651
private static short ByteToShort(byt e 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(byt e 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.o rg> wrote in message
news:eI******** ******@TK2MSFTN GP11.phx.gbl...
private static short ByteToShort(byt e 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.o rg> wrote:
private static short ByteToShort(byt e 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(byt e 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
7904
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" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
4
10413
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. I can assign the character data by the byte. However, I'm not sure how I would assign a binary value to a certain location in a byte array where the value is greater than 255. I have an unsigned short and want to assign it to a
3
10356
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, etc. I need: short = byte+byte, short = byte+byte, etc.
2
2331
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 = myFile->get_Length(); Byte myFileData; myFile1Data = new Byte;
2
1408
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; // 0 1 byte
9
16815
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 argument. Bitconverter.GetBytes( short ); -- Alberto Cardoso
3
2231
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, I get the data on a byte array which I successfully convert to an object with similarities to the object the data came from in C++, then I construct an object with the reply, encode it to a byte array and I send it back to the client in C++ Here I...
11
1784
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 time i perform an operation. Example: private byte a, b; public byte MyProperty { get { return (byte)(a + b); } }
10
2897
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 show the code first.. private static extern int IntADsOpenObject(string path, string userName, string password, int flags,ref MyGuid iid, out object ppObject); struct MyGuid { public Int32...
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.