What I understand that hexadecimal numbers can be placed into string using \x
. For example 0x41 0x42
can be placed in string as "\x41\x42"
.
char * ptr = "\x41\x42" ;
printf( "%s\n" , ptr ) // AB
\x
is discarded and 41
is considered as a hex by the compiler.
But if I pass it to my program through command line arguments, it does not work.
// test.c
main( int argc , char * argv[] )
{
printf( "%s\n" , argv[1] ) ;
}
$ gcc -o prog test.c
$ ./prog "\x41\x42"
\x41\x42
$ .prog \x41\x42
\x41\x42
What I expected was AB
like in the example 1.
Why is it so? Why this method of representation does not work in case of command line arguments?
How can value in argv[1]
which we know for sure is a hex string can be converted to hex number (without parsing, like done in the first example)?
Thanks for your time.
It does work in source code because compiler(preprocessor) does the replacement. When your program work alone in the dark, there is no compiler to help to do that kind replacement.
So, if you need to parse that like compiler does - do it yourself.
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