Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why hex in string is not converted to hex when passed through command line argument?

Tags:

c

string

hex

argv

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.

like image 832
Andrew-Dufresne Avatar asked Dec 13 '22 19:12

Andrew-Dufresne


1 Answers

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.

like image 138
BarsMonster Avatar answered Apr 09 '23 13:04

BarsMonster