How to define hex character array in C?
Hi group,
I have a simple question. I want to define a hex character array in C, but I don't want to have to define the elements one at a time. example: char array[] = "xxzzy'; is valid. but char array[] = '0x303233'; is not. Any ideas? Thanks for your time. |
I'm a little confused. What are you trying to do with them?
If you're just trying to store hex numbers so that they look like '0x303233' and make sure that they are valid, use a function that is called on candidate strings to see if they are 'valid' or not. If you're just trying to store hex numbers, you can store a hex number in an integer by simply doing this: Code:
int hexNum = 0x303233; Does that help? |
for example,
Code:
char array[] = "abc"; |
Actually the first is not valid because you open with a double quote and close with a single quote. But typos aside what are you exactly trying to achieve?
As CRC123 said the hex format is a number representation, because of that the language doesn't support simple assignment into a string. But the string can be used as a storage of hex numbers, essentially that is what a binary file is. Knowing where you data is coming from may assist in addressing your problem. |
Quote:
Code:
int hexNum = 0x303233; Really the only way to insert binary data into a string is with \x, otherwise you have to create the entire string as an array of binary values. But if you don't need to use the data as a string, i.e. don't need an arbitrary-length set of byte-wise data, you can use other methods of storage. ta0kira |
Quote:
Rather than reading in a sound file, I want it stored with in the address space. So what I need is an array of data bytes, not numbers. I guess using only 4 bytes in my example was confusing? A better example would be: char Array[] =I don't want to have to say '0x80', '0x7f', '0x80', etc."\0x7f7f7f7f7f7f7f7f7f7f7f7f" I plan to use a program to convert a raw sound file to this C source code. |
Do you have any example code of what you already have?
So you trying to read in a binary file into memory and store it into a char array? |
Quote:
char *soundbuf, *jj; int soundbufsize,ii; (now read your raw sound file, get the size of the file, allocate storage for the buffer, with the soundbuf pointer as the location, then fill the soundbuf buffer). now something like this: Code:
jj = soundbuf; run this from the console, redirect to a file and voila! Instant C code. |
If you are trying to "hard code" bytes into a C source code file representing the contents of a sound file - (I don't know why you'd want to) - then you'll have to obey the rules of initialisation of variables.
If you want to just play the file or something and can read from a separate file then the supplied solutions will suffice. Given that you may wish to do the former then here is a sample C program to illustrate your choices with hard coding the arrays/strings: Code:
/* Show byte initialisation for: Again it is far easier to have a seperate file containing the data and just to read that to a memory buffer. |
Quote:
End |
All times are GMT -5. The time now is 08:50 PM. |