如何使用十六进制数字初始化char数组? [英] How to initialize char array using hex numbers?
问题描述
我使用utf8,必须在char数组中保存一个常量:
I use utf8 and have to save a constant in a char array:
const char s[] = {0xE2,0x82,0xAC, 0}; //the euro sign
但是它给了我错误:
test.cpp:15:40: error: narrowing conversion of ‘226’ from ‘int’ to ‘const char’ inside { } [-fpermissive]
我必须将所有的十六进制数字都转换为char,这让我感到乏味并且不好闻。还有其他适当的方法吗?
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
可能是 signed
或 unsigned
(默认值是特定于实现的)。您可能想要
char
may be signed
or unsigned
(and the default is implementation specific). You probably want
const unsigned char s[] = {0xE2,0x82,0xAC, 0};
或
const char s[] = "\xe2\x82\xac";
(a 字符串文字是 char
的数组,除非您给它加上一些前缀)
(a string literal is an array of char
unless you give it some prefix)
请参见 -funsigned-char (或 -fsigned-char GCC的
)选项。
在某些实现中, char
是 unsigned
和 CHAR_MAX
为255(而 CHAR_MIN
为0)。在其他字符上, char
-s是签名的
,因此 CHAR_MIN
是- 128和 CHAR_MAX
是127(例如,Linux / PowerPC / 32位和Linux / x86 / 32位是不同的)。 AFAIK标准中没有任何内容禁止使用19位带符号的字符。
On some implementations a char
is unsigned
and CHAR_MAX
is 255 (and CHAR_MIN
is 0). On others char
-s are signed
so CHAR_MIN
is -128 and CHAR_MAX
is 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.
这篇关于如何使用十六进制数字初始化char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!