How does "\ x" work in a string?

I am writing a C / C ++ program that includes representing a hexadecimal number in a string, and I am confused about how it works \x. I saw examples where people write things like "\ xb2". In this case, how does the program know if you want hexadecimal b followed by number 2, or if you want the hexadecimal element b2? Also, when it stores this in a memeory, does it save the characters "\ x" or just save the hexadecimal representation?

+5
source share
4 answers

From standard C99 (6.4.4.4):

escape- - , escape-.

+6

"123\x45" 31 32 33 45.

Oli "\ x".

"\ x" . escape- , , . , '\n' , 0x0A.

+3

escape-\x , , \x, .

, "ABC" "\ x414243"

-, % x % X:

printf("%X%X%X", 'A', 'B', 'C');    // emits "414243"

. 1.2.6 1.2.7 C

, .

0

The translation is done at compile time, so each line that you manually enter into the source code with \x, ends up being a character that it represents in binary format. If you want to do this at runtime, you will need to call a parsing function, for example strtol(), using base 16, passing a string containing hexadecimal code, and sending it to char.

0
source

All Articles