C++ 如何使用十六进制数初始化字符数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19715439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:04:58  来源:igfitidea点击:

How to initialize char array using hex numbers?

c++unicode

提问by texasbruce

I use utf8 and have to save a constant in a char array:

我使用 utf8 并且必须在 char 数组中保存一个常量:

const char s[] = {0xE2,0x82,0xAC, 0}; //the euro sign

However it gives me error:

但是它给了我错误:

test.cpp:15:40: error: narrowing conversion of ‘226' from ‘int' to ‘const char' inside { } [-fpermissive]

I have to cast all the hex numbers to char, which I feel tedious and don't smell good. Is there any other proper way of doing this?

我必须将所有十六进制数字转换为 char,我觉得这很乏味,而且味道不好。有没有其他正确的方法来做到这一点?

回答by Basile Starynkevitch

charmay be signedor unsigned(and the default is implementation specific). You probably want

char可能是signedunsigned(并且默认值是特定于实现的)。你可能想要

  const unsigned char s[] = {0xE2,0x82,0xAC, 0}; 

or

或者

  const char s[] = "\xe2\x82\xac";

(a string literalis an array of charunless you give it some prefix)

字符串文字是一个数组,char除非你给它一些前缀)

See -funsigned-char(or -fsigned-char) option of GCC.

请参阅GCC 的 -funsigned-char(或-fsigned-char)选项。

On some implementations a charis unsignedand CHAR_MAXis 255 (and CHAR_MINis 0). On others char-s are signedso CHAR_MINis -128 and CHAR_MAXis 127 (and e.g. things are different on Linux/PowerPC/32 bits and Linux/x86/32 bits). AFAIK nothing in the standard prohibits 19 bits signed chars.

上一些实施方式中charunsignedCHAR_MAX是255(和CHAR_MIN为0)。别人char-s是signed如此CHAR_MIN是-128和CHAR_MAX127(和如东西都是在Linux / PowerPC的/ 32位和Linux / x86平台/ 32位不同)。AFAIK 标准中没有任何内容禁止 19 位有符号字符。

回答by Zac Howland

The short answer to your question is that you are overflowing a char. A charhas the range of [-128, 127]. 0xE2 = 226 > 127. What you need to use is an unsigned char, which has a range of [0, 255].

您的问题的简短答案是您正在溢出一个char. Achar的范围为 [-128, 127]。0xE2 = 226 > 127. 你需要使用的是一个unsigned char,它的范围是 [0, 255]。

unsigned char s = {0xE2,0x82,0xAC, 0};

回答by Mike Layton

While it may well be tedious to be putting lots of casts in your code, it actually smells extremely GOOD to me to use as strong of typing as possible.

虽然在您的代码中放置大量强制转换可能很乏味,但使用尽可能强的类型对我来说实际上闻起来非常好。

As noted above, when you specify type "char" you are inviting a compiler to choose whatever the compiler writer preferred (signed or unsigned). I'm no expert on UTF-8, but there is no reason to make your code non-portable if you don't need to.

如上所述,当您指定类型“char”时,您是在邀请编译器选择编译器编写者喜欢的任何内容(有符号或无符号)。我不是 UTF-8 方面的专家,但如果您不需要,没有理由让您的代码不可移植。

As far as your constants, I've used compilers that default constants written that way to signed ints, as well as compilers that consider the context and interpret them accordingly. Note that converting between signed and unsigned can overflow EITHER WAY. For the same number of bits, a negative overflows an unsigned (obviously) and an unsigned with the top bit set overflows a signed, because the top bit means negative.

就您的常量而言,我使用了以这种方式将默认常量写入带符号整数的编译器,以及考虑上下文并相应地解释它们的编译器。请注意,有符号和无符号之间的转换可能会溢出。对于相同的位数,负数溢出无符号数(显然),无符号数的最高位设置溢出有符号数,因为最高位表示负数。

In this case, your compiler is taking your constants as unsigned 8 bit--OR LARGER--which means they don't fit as signed 8 bit. And we are all grateful that the compiler complains (at least I am).

在这种情况下,您的编译器将您的常量视为无符号 8 位 - 或更大 - 这意味着它们不适合有符号 8 位。我们都很感激编译器抱怨(至少我是)。

My perspective is, there is nothing at all bad about casting to show exactly what you intend to happen. And if a compiler lets you assign between signed and unsigned, it should require that you cast regardless of variables or constants. eg

我的观点是,用演员表来准确地展示你打算发生的事情并没有什么坏处。如果编译器允许您在有符号和无符号之间进行分配,则它应该要求您在不考虑变量或常量的情况下进行转换。例如

const int8_t a = (int8_t) 0xFF; // will be -1

const int8_t a = (int8_t) 0xFF; // 将是 -1

although in my example, it would be better to assign -1. When you are having to add extra casts, they either make sense, or you should code your constants so they make sense for the type you are assigning to.

尽管在我的示例中,最好分配 -1。当您必须添加额外的强制转换时,它们要么有意义,要么您应该对常量进行编码,以便它们对您分配给的类型有意义。