Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Converting Hex data to Decimal in WAVE files.

Name: Anonymous 2012-06-09 22:22

Alright so what I'm trying to do is read the data chunks from a .WAV file and convert each chunk to decimal (0 - 65535 was the original range) so that I can manipulate it and use it with my micro controller. Anyway, I'm using fstream and reading the entire contents to a memory block, then unloading the file and transferring data from the memory block to the new file. There only seems to be one problem, the strtol function is not converting the hex data to decimal. I believe it may be because the hex data is being read as ASCII rather than hex values, i.e.  ÷¾ instead of f7be. This is the code I am using to process the data.

for (int i=44; i<dloop; i=i+2)
    {
        // Prepare hex values
        temp[0] = memblock[i+1];
        temp[1] = memblock[i];
        temp[2] = 0;

        // Convert hex to dec
        conv = strtol(temp,&pEnd,16);
        save << temp << " " << conv << "\n";
    }

It outputs the raw hex code then puts a space, then outputs the converted decimal values. This is the output.

ök 0
÷ 0
÷¾ 0
ø+ 0
øT 0

etc
Any suggestions? Also the reversing of the hex bytes before converting is intentional.

Name: Anonymous 2012-06-10 0:14

>>1
I'm not so sure I understand what you're trying to do. According to this:
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html

.wav stores binary binary data in the little endian order, rather than text data. So you have no reason to use strtol, which converts text data to long ints.

Typically you'd just read the data into an array of char (with the assumption that CHAR_BIT == 8) using a function like fread(), then convert the data from the array of char to an unsigned integer (in the host format) which is large enough to store the result.

Consider using the following macros for packing an integer in the host format, into an array 2 of char in the little endian order (which can be used with fwrite()):


#define packle16(b, n) \
        do {                                    \
                (b)[0] = (n) & 0xff;            \
                (b)[1] = (n) >> 8 & 0xff;       \
        } while(0)


And this one for unpacking an array 2 of char in the little endian order into the host format:


#define unpackle16(b) ((unsigned int) (b)[1] << 8 | (unsigned int) (b)[0])

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List