How does “\x” work in a String?

后端 未结 4 629
一生所求
一生所求 2021-01-02 14:06

I\'m writing a C/C++ program that involves putting a hex representation of a number into a string and I\'m confused as to how \\x works. I\'ve seen examples wh

4条回答
  •  孤独总比滥情好
    2021-01-02 15:10

    When you use the escape sequence \x inside a string the data following the \x is actually stored in it's binary representation.

    So the string "ABC" is equivalent to the string "\x414243"

    If you want to emit hexadecimal values in display-character form, you'll want to use the %x or %X format specifier character:

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

    See Section 1.2.6 and Section 1.2.7 of the C Library Reference Guide

    Hope that explanation helps.

提交回复
热议问题