Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HEX assignement in C

Tags:

c++

c

I have generated a long sequence of bytes which looks as follows:

0x401DA1815EB560399FE365DA23AAC0757F1D61EC10839D9B5521F.....

Now, I would like to assign it to a static unsigned char x[].

Obviously, I get the warning that hex escape sequence out of range when I do this here

static unsigned char x[] = "\x401DA1815EB56039.....";

The format it needs is

static unsigned char x[] = "\x40\x1D\xA1\x81\x5E\xB5\x60\x39.....";

So I am wondering if in C there is a way for this assignment without me adding the hex escape sequence after each byte (could take quite a while)

like image 978
Mareika Avatar asked Dec 07 '22 04:12

Mareika


2 Answers

I don't think there's a way to make a literal out of it.

You can parse the string at runtime and store it in another array.

You can use sed or something to rewrite the sequence:

echo 401DA1815EB560399FE365DA23AAC0757F1D61EC10839D9B5521F | sed -e 's/../\\x&/g'
\x40\x1D\xA1\x81\x5E\xB5\x60\x39\x9F\xE3\x65\xDA\x23\xAA\xC0\x75\x7F\x1D\x61\xEC\x10\x83\x9D\x9B\x55\x21F
like image 157
strager Avatar answered Dec 24 '22 23:12

strager


AFAIK, No.

But you can use the regex s/(..)/\\x$1/g to convert your sequence to the last format.

like image 28
kennytm Avatar answered Dec 25 '22 00:12

kennytm