ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
The internal representation of everything is the same; it's just a matter of interpreting it, which is what C provides:
Code:
int hexNum = 0x303233;
printf("%i\n", hexNum);
printf("0x%.6x\n", hexNum);
printf("%.3s\n", &hexNum);
You really can't tell how much C actually provides automatically until you look at it that way. You just need to know what format you need to use your data in, then follow the C-based rules associated with adding that data to the program.
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
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;
If you print it, it will say: 3158579
Does that help?
No this is not what I want. I want an in storage array of data bytes.
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:
No this is not what I want. I want an in storage array of data bytes.
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:
I don't want to have to say '0x80', '0x7f', '0x80', etc.
I plan to use a program to convert a raw sound file to this C source code.
What is so hard about that? You have to read your raw sound file into your translation program, of course, but then you translate it something like this (note: this is off the top of the head and NOT tested):
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).
This will output an array definition with your bytes represented in hex. I think it will also leave a trailing comma in the file but you could edit that out manually.
run this from the console, redirect to a file and voila! Instant C code.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
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:
* 1) A string of characters (note null terminator added by complier)
* 2) An array of one byte values (characters) initialised with 0x terminology
* 3) An array of one byte values (characters) initialised with '\x' terminology
*/
#include <stdio.h>
void myprinthex(char[]);
int main(int,char *[]);
int main(int argc, char *argv[])
{
char myhexarray1[]={"\x1\x2\x3\x4"}; /* Character string initialised to bytes habving value 1,2,3,4 */
char myhexarray2[]={0x1,0x2,0x3,0x4} ; /* Array of characters initialised to bytes having value 1,2,3,4 */
char myhexarray3[]={'\x1','\x2','\x3','\x4'}; /* Array of character - different notation - initialsed 1,2,3,4 */
myprinthex(myhexarray1);
myprinthex(myhexarray2);
myprinthex(myhexarray3);
return 0;
}
void myprinthex(char myarray[]) /* Print of hexidecimal representation of arrays or strings */
{
int i;
for (i=0;i<sizeof(myarray);i++)
{
printf ("Byte %d of array of length %d has hex value 0x%x\n",i+1,sizeof(myarray),myarray[i]);
}
printf("\n");
}
I'll leave it up to you to figure out how to read in a file and print the contents to a C source code file.
Again it is far easier to have a seperate file containing the data and just to read that to a memory buffer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.