- Talking C a
char is the smallest integer value available.
- Talking C and ASCII a character literal (like
'A') is just another representation of an int value (65 here).
- Talking Math there are other ways to represent the very same value, aside the decimal way (using base 10). One common is using the base of 16, the hexadecimal way.
Putting the above together it proves that:
int i = 'A';
char c = i;
is the same as:
int i = 65;
char c = i;
and the same as:
int i = 0x41;
char c = i;
and the same as:
char c = 'A';
and the same as:
char c = 65;
and the same as:
char c = 0x41;
So
char a[5] = "ABCDE";
in fact already is an integer array of five (small) integers.
If printed using the correct conversion specifier and length modifier you see what you are after.
for (size_t i = 0; i < 5; ++i)
{
printf("%hhx ", a[i]);
}
Output:
41 42 43 44 45
To indicate to the reader this should be taken as hexadecimal values one could prefix the value using the common 0x notation
printf("0x%hhx ", a[i]);
which gave you
0x41 0x42 0x43 0x44 0x45
If you would show this to Pascal guys you perhaps would use
printf("$%hhx ", a[i]);
which gave you
$41 $42 $43 $44 $45
To see the characters themselves just us:
printf("%c ", a[i]);
and get
A B C D E
To see the decimal values use
printf("%d ", a[i]);
and get
65 66 67 68 69
The conclusion is, that it's all just matter of how you represent ("print") the same values (the content of a's elements) . The "conversion" happens when creating the representation of a very same value (via printing and passing the right "instructions"), that is during output only.
As you refer to ASCII only, this implies all value are <128, so you can just plain copy the array's values using either a loop
char src[5] = "ABCDE";
uint8_t dst[5];
for (size_t i = 0 i < 5; ++i)
{
dst[i] = src[i];
}
or by copying the related memory block at once
memcpy(dst, src, 5);
uint8_tcan't contain hex values; and"ABCDE"is equivalent to{0x41,0x42,0x43,0x44,0x45,0}. Just cast. (Also as a sidenote,"ABCDE"doesn't fit intoarray[5], as it has 6 elements, because strings are NUL-terminated, as shown in the equivalent byte array."ABCDE"is an array of 6chars as there is added a 0 terminator. I'm not sure whether the compiler complains or implicitly fixeschar array[5] = "ABCDE";. However, you may access the first 5chars. The expected output are already the corresponding ASCII values (as already pointed out) andsizeof (char) == sizeof (uint8_t). So, this can be reduced to a simple copy:memcpy(output, array, 5);. (But, if you can't usememcpy()for any reasons a simpleforloop should do as well.)char array[5] = "ABCDE";- I checked this out: The compilers (gcc HEAD 9.0.0, clang HEAD 7.0.0, both with-pedantic) don't complain but fixes this implicitly: Test on Wandbox. (Early in the morning but already learned something...) ;-)array[5] = "ABCDE"is special. It defines (and initialises) achar-array with five elements (only). No0-terminator here. This is in line with the C11 standard.