LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2008, 07:00 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
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.
 
Old 09-07-2008, 08:24 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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?
 
Old 09-07-2008, 09:01 PM   #3
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
for example,

Code:
char array[] = "abc";

// is equal to

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

Last edited by fantas; 09-07-2008 at 09:22 PM.
 
Old 09-07-2008, 10:59 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 09-07-2008, 11:47 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by CRC123 View Post
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
 
Old 09-08-2008, 01:08 PM   #6
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by CRC123 View Post
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.
 
Old 09-08-2008, 01:55 PM   #7
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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?
 
Old 09-08-2008, 02:27 PM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Post

Quote:
Originally Posted by MrUmunhum View Post
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.
 
Old 09-08-2008, 03:33 PM   #9
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 09-08-2008, 07:16 PM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html hex representation of character kpachopoulos Programming 2 06-12-2007 10:39 PM
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM
SIGSEGV: Array of character pointers?? usercsr Programming 2 04-23-2005 11:34 AM
C can 't return a character array Linh Programming 5 06-18-2003 04:58 AM
Cannot pass and return a character array Linh Programming 1 06-12-2003 04:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration