C initialize array in hexadecimal values
Asked Answered
S

6

8

I would like to initialize a 16-byte array of hexadecimal values, particularly the 0x20 (space character) value.

What is the correct way?

unsigned char a[16] = {0x20};

or

unsigned char a[16] = {"0x20"};

Thanks

Susceptible answered 31/10, 2015 at 18:17 Comment(3)
0x2 is not the space character, but an integer.Tansey
I am talking about the ASCII space character in hex, which is 0x20Susceptible
The C standard does not use ASCII. (Just noted I missed a trailing 0; I meant 0x20, of course). If you mean space, you should use a space character constant: ' '.Tansey
P
7

There is a GNU extension called designated initializers. This is enabled by default with gcc

With this you can initialize your array in the form

unsigned char a[16] = {[0 ... 15] = 0x20};
Polygenesis answered 31/10, 2015 at 18:31 Comment(1)
Designated initialisers are not a gcc extension, but standard C. Allowing a range is an extension, however is..Tansey
H
8

Defining this, for example

unsigned char a[16] = {0x20, 0x41, 0x42, };

will initialise the first three elements as shown, and the remaining elements to 0.

Your second way

unsigned char a[16] = {"0x20"};

won't do what you want: it just defines a nul-terminated string with the four characters 0x20, the compiler won't treat it as a hexadecimal value.

Hymenium answered 31/10, 2015 at 18:21 Comment(2)
Ahh ok I see. And what if I want the ASCII value of 0x20? Like I want the keyboard space that that hex value gives.Susceptible
@Susceptible the ASCII value is 0x20 (hex) or 32 (decimal). If you print that as an int you'll get 32 and if you print as char you'll get ` ` (space).Hymenium
P
7

There is a GNU extension called designated initializers. This is enabled by default with gcc

With this you can initialize your array in the form

unsigned char a[16] = {[0 ... 15] = 0x20};
Polygenesis answered 31/10, 2015 at 18:31 Comment(1)
Designated initialisers are not a gcc extension, but standard C. Allowing a range is an extension, however is..Tansey
P
5
unsigned char a[16] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};

or

unsigned char a[16] = "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20";
Preliminary answered 31/10, 2015 at 18:23 Comment(0)
W
4

I don't know if this is what you were looking for, but if the size of the given array is going to be changed (frequently or not), for easier maintenance you may consider the following method based on memset()

#include <string.h>

#define NUM_OF_CHARS 16

int main()
{
    unsigned char a[NUM_OF_CHARS];

    // initialization of the array a with 0x20 for any value of NUM_OF_CHARS
    memset(a, 0x20, sizeof(a));

    ....
}
Walrus answered 31/10, 2015 at 18:33 Comment(0)
G
3

The first way is correct, but you need to repeat the 0x20, sixteen times. You can also do this:

unsigned char a[16] = "                ";
Graniteware answered 31/10, 2015 at 18:22 Comment(5)
This solution will lead to undefined behavior! "<16 spaces>" will actually occupy 17 bytes (the last one for '\0').Walrus
@AlexLop. Negative, the C specification allows a character array to be initialized using a string literal that is the same size as the array. In that case the NUL terminator is dropped.Graniteware
Could you please point me to the documentation which will support your statement?Walrus
Section 6.7.9 paragraph 14, which says (emphasis added): "Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array."Graniteware
You are right, my mistake. Could you please make a small edit in your answer so I could "undo" the downvote?Walrus
M
0

I would use memset.

No need to type out anything and the size of the array is only provided once at initialisation.

#include <string.h>

int main(void)
{
  unsigned char b[16];
  memset(b, 0x20, sizeof(b));
}
Mensurable answered 16/4, 2020 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.