Converting Hex data to Decimal in WAVE files.
1
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.
2
Name:
Anonymous
2012-06-09 23:33
I know this is a slow moving board, but can I get some help with this? Surely someone knows an easy way around this?
3
Name:
Anonymous
2012-06-09 23:34
HOW ABOUT NO
4
Name:
Anonymous
2012-06-09 23:39
this board is not about programming, it just appears to be.
5
Name:
Anonymous
2012-06-09 23:43
>>4
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
6
Name:
Anonymous
2012-06-09 23:44
>>5
fuck off and die in a fire you cock sucking faggot.
7
Name:
Anonymous
2012-06-09 23:44
>>6
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
8
Name:
Anonymous
2012-06-09 23:50
>>7
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
9
Name:
Anonymous
2012-06-09 23:50
Wow stop being arseholes, this dude comes in asking for help and none of you comply?
You wonder why we don't get much traffic.
Also I cannot help you as I do not do C related languages, sorry.
10
Name:
Anonymous
2012-06-09 23:51
>>9
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
11
Name:
Anonymous
2012-06-09 23:56
>>10
I AM a FEM PROGRAMMER you WORTHLESS PIECE OF SHIT, now shut the fuck up.
12
Name:
Anonymous
2012-06-09 23:57
>>11
WATCH YOUR PRIVILEGE, FEMALE PROGRAMMER.
13
Name:
Anonymous
2012-06-09 23:58
>>12
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
14
Name:
Anonymous
2012-06-10 0:05
wow fuck you all I'm going to go ask /g/, cunts.
15
Name:
Anonymous
2012-06-10 0:06
>>14
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
16
Name:
Anonymous
2012-06-10 0:08
fucking slow ass spambot
17
Name:
Anonymous
2012-06-10 0:10
>>16
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
18
Name:
Anonymous
2012-06-10 0:12
>>14
ask anyone you want fucking faggot, now GTFO already.
19
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])
20
Name:
Anonymous
2012-06-10 0:14
21
Name:
Anonymous
2012-06-10 0:15
>>20
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
22
Name:
19
2012-06-10 0:26
Here's an example of reading a 16-bit little endian integer from stdin, converting it to the host order, then printing it in hexadecimal and decimal.
#include <stdio.h>
#define unpackle16(b) ((unsigned int) (b)[1] << 8 | (unsigned int) (b)[0])
int main(void)
{
unsigned char data[2];
unsigned int n;
if (fread(data, sizeof data, 1, stdin) != 1)
return 0;
n = unpackle16(data);
printf("%u %x\n", n, n);
return 0;
}
23
Name:
Anonymous
2012-06-10 0:28
>>20
It's nice to have a balance of helpful posts and shitposts. ^_^
24
Name:
Anonymous
2012-06-10 0:28
>>22
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
25
Name:
Anonymous
2012-06-10 0:29
>>23
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
26
Name:
Anonymous
2012-06-10 0:30
thanks for nothing!
27
Name:
Leah
2012-06-10 0:33
fuck this board is dead as fuck
28
Name:
Anonymous
2012-06-10 0:35
Hey OP here, glad I didn't close this tab, /g/ was no help. So thanks to the guy/girl who actually posted an example!
29
Name:
Anonymous
2012-06-10 0:39
>>28
fuck you faggot im not a girl
30
Name:
Anonymous
2012-06-10 0:41
31
Name:
Anonymous
2012-06-10 1:02
>>29
People on here are bitching about men so what ever, had to accommodate for both.
>>30
Is this a joke?
32
Name:
Anonymous
2012-06-10 1:03
>>31
JUST BE SURE TO CHECK YOUR PRIVILEGE FAGGOT
>>33
nice doubles, faggot
33
Name:
Anonymous
2012-06-10 1:04
>>32
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
34
Name:
Anonymous
2012-06-10 1:04
>>32
Fuck off, I do what I want.
35
Name:
Anonymous
2012-06-10 1:05
>>33
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
36
Name:
Anonymous
2012-06-10 1:05
>>35
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
37
Name:
Anonymous
2012-06-10 1:06
Oh shit the spambot is spamming itself
38
Name:
Anonymous
2012-06-10 1:06
>>37
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
39
Name:
Anonymous
2012-06-10 1:08
>>38
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
40
Name:
Anonymous
2012-06-10 1:09
FUCKING SEXIST PIGS. WOMEN CAN BE PROGRAMMERS TOO.
41
Name:
Anonymous
2012-06-10 1:10
>>40
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
42
Name:
Anonymous
2012-06-10 1:12
>>41
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
43
Name:
Anonymous
2012-06-10 1:16
>>42
WATCH YOUR PRIVILEGE, MALE PROGRAMMER.
44
Name:
Anonymous
2012-06-10 1:30
45
Name:
Anonymous
2012-06-10 1:42
WATCH YOUR PRIVILEGE, FEMINIST SPAMMER.
46
Name:
Anonymous
2012-06-10 2:05
NYJMUA
47
Name:
Anonymous
2012-06-10 2:11
>>40
WATCH YOUR PRIVILEGE, FEMALE PROGRAMMER.
48
Name:
Cudder
!MhMRSATORI!FBeUS42x4uM+kgp
2012-06-10 8:45
save << temp << " " << conv << "\n";
This is what happens when we teach high-level languages before basic stuff like data representation.
OP needs to learn a whole lot more of the latter first.