Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


2 byte integer / .wav data -  sample value  conversion

Posted on 2003-03-10
Medium Priority
Last Modified: 2009-07-29
Hello ,

I'm trying to simply display a wave file graphically ...
I'm stuck in reading the  bytes and convert it to a sample value :
ie. for 16bit PCM , I have to convert 2 bytes to a value ranging from -32768 to 32767

so , I have the 2 byte values , ranging from 0 to 255 which represent the 65536-step value , but I dont know how to convert them ,
I guess i could just multiply them, so I have 256 * 256 (=65536) possible values ,
but that must be not correct , cause I have values like (0 and 9) or (0 and 200) which is multiplied =>> 0 * 9 = 0 ???

My guesss is that I have to read as a byte ranging from 0x7FFF(=32767) to 0x8000(-32768),
a 2 byte integer ... but
how do you do that ??
how do you read 2 byte integers ??
Question by:DotNetSlet
LVL 44

Accepted Solution

Arthur_Wood earned 225 total points
ID: 8104136
what is normally done is to treat these as a "HIGH ORDER" and "LOW ORDER" pair, and then MULIPLY the High-Order value by 256, and Add the Low-Order value, ending up with a value formatted as HHHHLLLL.


Expert Comment

ID: 8104271
Maybe I don't understand well what you need. But if you
need to read a file as array of short values here is a solution:

System.IO.FileStream a = System.IO.File.OpenRead(@"C:\Program Files\NetMeeting\Blip.wav");

short[] c = new short[50];
byte[] bp = new byte[100];
a.Read(bp, 0, 100);

fixed(byte* pSrc = bp) {
  fixed(short* pDst = c) {
    byte* ps = pSrc;
    short* pd = pDst;
    for (int n = 0; n < 50; n++) {
      *((short*)pd) = *((short*)ps);
      pd ++;
      ps += 2;

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question