LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to define hex character array in C? (https://www.linuxquestions.org/questions/programming-9/how-to-define-hex-character-array-in-c-668213/)

MrUmunhum 09-07-2008 07:00 PM

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.

CRC123 09-07-2008 08:24 PM

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?

fantas 09-07-2008 09:01 PM

for example,

Code:

char array[] = "abc";

// is equal to

char array[] = "\x61\x62\x63";


graemef 09-07-2008 10:59 PM

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.

ta0kira 09-07-2008 11:47 PM

Quote:

Originally Posted by CRC123 (Post 3272751)
Code:

int hexNum = 0x303233;
If you print it, it will say: 3158579

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

MrUmunhum 09-08-2008 01:08 PM

Quote:

Originally Posted by CRC123 (Post 3272751)
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:
char Array[] =
"\0x7f7f7f7f7f7f7f7f7f7f7f7f"
"\0x7f7f7f7f7f7f7f7f7f7f7f7f"
"\0x808182838281"
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.

CRC123 09-08-2008 01:55 PM

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?

jiml8 09-08-2008 02:27 PM

Quote:

Originally Posted by MrUmunhum (Post 3273585)
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:
char Array[] =
"\0x7f7f7f7f7f7f7f7f7f7f7f7f"
"\0x7f7f7f7f7f7f7f7f7f7f7f7f"
"\0x808182838281"
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).

now something like this:
Code:

jj = soundbuf;
printf("soundrec[%d] = ",soundbufsize);
while(jj < soundbuf + soundbufsize)
{
    for(ii=0;ii<20;ii++)
    {
        printf("0x%x,",*jj);
        jj++;
    }
    if(jj < soundbuf + soundsize){printf("\ \n");}
    else {printf("; \n");}
}

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.

bgeddy 09-08-2008 03:33 PM

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.

AnanthaP 09-08-2008 07:16 PM

Quote:

char array[] = "xxzzy'; is valid.
but
char array[] = '0x303233'; is not.
Huh, I thought hex characters can contain only 0 to 9 and a to f.

End


All times are GMT -5. The time now is 08:50 PM.