I usually use pointers in the following manner
char *ptr = malloc( sizeof(char) * 100 ); memset( ptr, 0, 100 ) ; strncpy( ptr, "cat" , 100 - 1 );
But this time instead of using "cat", I want to use it ASCII equivalent in hex.
cat = 0x63, 0x61, 0x74, 0x00
I tried
strncpy( ptr, "0x630x61" , 100 - 1 );
But it fails as expected.
What is the correct syntax?
Do I need to put a 0x00 too? For a moment lets forget about memset
, now do I need to put a 0x00? Because in "cat" notation, a null is automatically placed.
Regards
Note, you only need \ inside the " " string
char cat[4];
cat[0] = 0x63;
cat[1] = 0x61;
cat[2] = 0x74;
car[3] = 0x00;
char cat[] = "\x63\x61\x74"; // note the \0 is added for you
char cat[] = { 0x63, 0x61, 0x74, 0x00 };
Are all the same
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With