By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,230 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,230 IT Pros & Developers. It's quick & easy.

Pls Help! - convert 2 bytes into 2-byte number

P: n/a
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[0], byte[1], byte[2], etc.

I need: short[0] = byte[0]+byte[1], short[1] = byte[2]+byte[3], etc.

So my problem is how do i correctly convert two bytes into a two-byte
number here?

This problem comes from using windows multimedia API wavIn* functions
and in particular callback function, which returns a recorded block of
data as IntPtr and i copy it using Marshal.Copy to byte array. But for
sound analysis, in case when i have 16 bit per second sound, i need to
get a 2-byte number out of the byte array.
Any ideas would be highly appreciated!

Thank you,
Andrey
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
MuZZy <le*******@yahoo.com> wrote:
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[0], byte[1], byte[2], etc.

I need: short[0] = byte[0]+byte[1], short[1] = byte[2]+byte[3], etc.
I presume you actually mean short[0] = byte[0]+byte[1]*256 etc.
So my problem is how do i correctly convert two bytes into a two-byte
number here?


Well, for a single one you can use BitConverter - and if that's in the
wrong endianness for you, you can use my EndianBitConverter which is
part of my MiscUtil library - see
http://www.pobox.com/~skeet/csharp/miscutil.

However, to convert a whole array of them, I'd suggest using
Buffer.BlockCopy. It will only work if the endianness is correct, but
should be faster than multiple BitConverter calls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Jon Skeet [C# MVP] wrote:
MuZZy <le*******@yahoo.com> wrote:
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[0], byte[1], byte[2], etc.

I need: short[0] = byte[0]+byte[1], short[1] = byte[2]+byte[3], etc.

I presume you actually mean short[0] = byte[0]+byte[1]*256 etc.

So my problem is how do i correctly convert two bytes into a two-byte
number here?

Well, for a single one you can use BitConverter - and if that's in the
wrong endianness for you, you can use my EndianBitConverter which is
part of my MiscUtil library - see
http://www.pobox.com/~skeet/csharp/miscutil.

However, to convert a whole array of them, I'd suggest using
Buffer.BlockCopy. It will only work if the endianness is correct, but
should be faster than multiple BitConverter calls.


Hi Jon,

I guess the endianness is correct as long as i get the byte data in the
same order as it's written to *.wav file.

But i don't see how Buffer.CopyBlock can help me - it copies int array
to int array, buut i need byte array to short array of half length.

Andrey
Nov 16 '05 #3

P: n/a
MuZZy <le*******@yahoo.com> wrote:
I guess the endianness is correct as long as i get the byte data in the
same order as it's written to *.wav file.
Goodo.
But i don't see how Buffer.CopyBlock can help me - it copies int array
to int array, buut i need byte array to short array of half length.


No, it copies part of *an array* to part of another array. Here's an
example of the kind you're interested in:

using System;

class Test
{
static void Main()
{
byte[] b = new byte[]{1, 2, 3, 4};
short[] s = new short[2];

Buffer.BlockCopy (b, 0, s, 0, 4);

Console.WriteLine ("{0} {1}", s[0], s[1]);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.