Skip to content
Advertisement

What’s the function of `’` in `printf “%x” “‘你”`?

I want to get the hexadecimal value of , someone tell me to use printf "%x" "'你", but I don’t know what’s the function of ' in printf "%x" "'你", why use ' before ?

Advertisement

Answer

From the bash manual:

Arguments to non-string format specifiers are treated as C constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character.

%x is a numeric specifier, not a string one, so this section applies. The documentation is a bit wrong (or outdated) when it speaks about ASCII values, but it’s correct in spirit: an argument of '你 evaluates to the numerical value of the unicode codepoint (without the quote, it would be a syntax error, since 你 isn’t a number). The codepoint value that it evaluates to is then formatted in hexadecimal by %x.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement