473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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

Similar topics

2
by: respect | last post by:
i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ] Example for input -6.5, the output should be. 1 10000001...
3
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
3
by: asazykin | last post by:
help need.... trying to convert binary file bytes 188-189 hav latitude values. how can i get from hex a6d4 - decodec to 217.200000 any ideas????would appreciate.....
1
by: programmerboy | last post by:
Hi, how do I convert floating point number to a date in ASP? Thanks
7
by: avanthika | last post by:
hi, I am using dropdownlist in webgrid.for loading the combo i am using this source code. Dim vallist As New Infragistics.WebUI.UltraWebGrid.ValueList Dim ds As New DataSet ...
1
by: rokenbok | last post by:
hi, I have a password field that I need to store in a db2 database. I created a blob field in my table, and I am trying to update it with the following java dal.getPrepared().setBytes(15,...
2
by: karanbikash | last post by:
hi , I have a column with datatype as decimal(31,2) . The data is like 123456.67 , 7634578.99 Is it a way to convert the entire number into character . like '1232456.67' . The output...
3
by: Sun | last post by:
Hi everyone . I have two files named a.txt and b.txt. I open a.txt with ultraeditor.exe. here is the first row of the file: neu für then I switch to the HEX mode: 00000000h: FF FE 6E 00 65...
7
by: Udara chathuranga | last post by:
How can I convert binary float number into a decimal float number ?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.