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

M

MuZZy

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
 
J

Jon Skeet [C# MVP]

MuZZy said:
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.
 
M

MuZZy

Jon said:
MuZZy said:
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
 
J

Jon Skeet [C# MVP]

MuZZy said:
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]);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top