0

Here I excute the following content

char* str="\x41\x41\x41\x41";
printf(str);

The print string should be: AAAA

I try to do the same thing using test.c, but the result is not "AAAA" but " \x41\x41\x41\x41"

C:\Users\hhcnb\Desktop\2021>test.exe \x41\x41\x41\x41
\x41\x41\x41\x41

How does this happen and how to solve it?

// test.c
#include <stdio.h>

int main(int argc ,char* argv[]) {
    printf(argv[1]);
    return 0;
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

"Compile time" and "run time" are two different beasts.

If you use the backslash in a string literal, it notifies the compiler that the following characters are to be treated in a special way. In your case, it shall read hex values and use them as characters.

But during run time, the compiler is not active any more. So it's up to your command prompt application to interpret the characters on the command line. Unfortunately Window's capabilities do not include such transformations (but see the linked question in a comment). So you get what you input.

To interpret such an input as hex, you need to program this yourself. There are multiple possibilities, which are left as an exercise to you.

the busybee
  • 10,755
  • 3
  • 13
  • 30