I am initializing a symlink in an ext2 inode (school assignment).
I got the idea to do it in hex
since the field is defined as uint32_t i_block[EXT2_N_BLOCKS].
As an example:
#include <stdio.h>
int main () {
// unsigned int is 32 bytes on my system
unsigned int i = 0x68656c6c; // hell
printf("%.*s\n", 4, &i");
I got the output
lleh
Is this because my system is little-endian? Does that mean if I hardcode the opposite order, it would not port to big-endian systems (my eventual goal is hello-world)?
What is the best, most simple way to store a character string into an array of unsigned integers?
Yes.
Code relying on the byte order of integers is non-portable indeed.
The best way is not to use integers at all but
char, which unlike integers does not depend on endianess and was actually designed for the purpose of storing characters.You could ignore that it is an integer type and just memcpy a string into it:
Or if you prefer:
memcpy(&i, "\x68\x65\x6c\x6c", 4);.Otherwise you'll have to invent some ugly hack like for example: