gpt4 book ai didi

c++ - 使用 HEX 定义使用 ESC 命令创建字符串

转载 作者:行者123 更新时间:2023-11-30 05:42:12 32 4
gpt4 key购买 nike

我需要用十六进制格式定义的特殊字符创建字符串:

std::string s = "\xFEd" ; 
error C2022: '65242' : too big for character

如果 std::string s = "\xFE d"; 一切正常。如何告诉字符串构造函数在第一种情况下将“d”视为字符串“d”而不是数字定义字符?

最佳答案

你在标签“visual C++”中写的,所以我假设 Microsoft 编译器来自 Visual Studio。

请查看引用资料和示例 at this site .它告诉你应该使用什么,但它似乎..有点错误。例如:

octal              \ooo
hexadecimal \xhhh <- note the 3 Hs
Unicode (UTF-8) \uxxxx
Unicode (UTF-16) \Uxxxxxxxx

和几个 pagedowns 之后,示例:

A hexadecimal escape sequence is a backslash followed by the character x, followed by a sequence of hexadecimal digits. An escape sequence that contains no hexadecimal digits causes compiler error C2153: "hex constants must have at least one hex digit". An escape sequence that has hexadecimal and non-hexadecimal characters is evaluated as the last non-hexadecimal character. The highest hexadecimal value is 0xff.

char c1 = '\x0050';  // char 'P'    <-- note 4 digits as 1 char
char c2 = '\x0pqr'; // char 'r'

注意 \x0pqr 这件事。乐趣!我怀疑编译错误,但没有!

(..)

我刚刚在 MSDN 上找到了确切的答案:short infolong info .后者摘录:

Using embedded hexadecimal escape codes to specify string constants can cause unexpected results. The following example seeks to create a string literal that contains the ASCII 5 character, followed by the characters f, i, v, and e:

"\x05five"

The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters i, v, and e. To get the correct result, you can use one of these:

"\005five"     // Use octal constant.  
"\x05" "five" // Use string splicing.

关于c++ - 使用 HEX 定义使用 ESC 命令创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749438/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com