![]() |
|
#1
|
|||
|
|||
Converting a BYTE array to a stringI am trying to read a wave file. However, in the code, it reads the RIFF header into a BYTE array using fread. It then compares the first 4 bytes, which should be "RIFF" if it is a wave file and continues if it is correct.
However, it uses strcmp, and that will not work with a BYTE array as an argument, is it possible to cast this to a string? The code I'm using is below, it is mostly taken from a tutorial found http://www.cpp-home.com/tutorial.php?333_1 CPP / C++ / C Code:
I'm using Visual Studio 2003, and the errors I get in compilation are: source.cpp(19): error C2664: 'strcmp' : cannot convert parameter 1 from 'BYTE [4]' to 'const char *' and the same for the strcmp on line 23, a similar scenario If you can give me any other helpful info, it would be appreciated too. |
|
#2
|
|||
|
|||
Re: Converting a BYTE array to a stringThere are a number of options as I see it. The simplest of which is to typecast the pointer to the BYTE array for the comparison (char *)id Is it always null terminated though? You assume that when you use strcmp. You probably want to use strncmp which allows you to specify the number of characters.
Hope this helps (and works!). Michael. __________________
Michael Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++). |
|
#3
|
|||
|
|||
Re: Converting a BYTE array to a stringQuote:
However... Quote:
Absolutely! That's the ticket. Regards, Dave |
|
#4
|
|||
|
|||
Re: Converting a BYTE array to a stringQuote:
According to a private communication I received, my previous post wasn't very clear. Sorry. I'll try again: BYTE is typedef'ed in as an unsigned char and C++ is very picky about matching argument types to function parameters. As MichaelS-R said, you could cast id (and, probably soundbuffer also) to (char *) at points where they are used. Or you could just declare them with char rather than BYTE (to make the C++ compiler happy). Anyhow, after getting the data types correct, the program will compile, but please see the previous notes about why strcmp is ***not*** valid with the program as written (with strcmp using the string literal "RIFF" and a 4-byte array). It won't give compile errors, but will result in undefined behavior at run time: might give the right answer; might give the wrong answer; might do something completely different (including bomb). The tutorial is just flat wrong in this respect. The narrative seems OK to me, and the intent of the code is clear enough, but I have to wonder why someone would go to the trouble of posting a tutorial with a complete routine of C code and then say, "there's probably a few errors in this code as I didn't have time to compile and test it." Regards, Dave |
|
#5
|
|||
|
|||
Re: Converting a BYTE array to a stringThanks for the replies in this thread. I have done as suggested, and cast the BYTE array as a char pointer. It now compiles fine. I had to make another change due to a run time error, which said that the variable size was used without being declared.
However, my code still will not read a wave file. The thing just crashes if it's a wave file. Here's my code below: CPP / C++ / C Code:
As I mentioned, the thing crashes if it's a wave file. Through some random comments put into the code, I know that the line: CPP / C++ / C Code:
is causing the crash (as it won't happen if it's commented out). However, is it a problem with this line? Or the line above relating to creating a soundbuffer? |
|
#6
|
|||
|
|||
Re: Converting a BYTE array to a stringQuote:
I pointed out that there was at least one bug in the link that you furnished, and another statement that, "there's probably a few errors in this code as I didn't have time to compile and test it." SO: 1. You know the program had a bug 2. The author admitted that he had not even compiled, much less, tested it. I was hoping that you would find other sources to verify what the heck your program is supposed to do (and it is your program now that you have entered and compiled the code with your computer). Bottom line: never use a single source for investigating. Look at other links. You could go to msdn.microsoft.com and enter ".wav file format" into the search engine there, or you could go to other sites, for example: http://ccrma.stanford.edu/courses/42...ts/WaveFormat/ If you look carefully, you will see that the item avg_bytes_sec should be read in as a 32-bit quantity (DWORD), not a short. This throws off other things below it. (It is declared as a DWORD, but only two bytes are read from the file.) Furthermore, the format given in your link only works for pcm files (where the format_tag has a value of 1). If you are working with any other format (adpcm or other kinds of compression) it simply won't work. It might read the first few headers OK, but you will then have to deal with some way of decompressing the actual sound data. Now, whether either or both of these have anything to do with your problem, I don't know, but both should be checked. Please, please (please) don't take my word for it. Check other resources on the web. I respectfully suggest that you study the information in the link that I gave, and when doing something new like this (reading a binary file with a particular format) that you print out exactly everything you read in from the file. I have made a couple of minor changes, but not to the actual reading part of your program Here's what I tried: CPP / C++ / C Code:
With a file named baloon.wav (from windows\media directory): Code:
With another file, start.wav from windows\media: Code:
Regards, Dave |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| [c++] byte manipulation in a hex string | czk101 | C++ Forum | 3 | 12-Feb-2006 11:12 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 09:14 |
| Converting string to float | CT++ | C++ Forum | 2 | 10-May-2005 12:29 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The