简体   繁体   English

如何在C ++中指定unsigned char类型的整数文字?

[英]How do I specify an integer literal of type unsigned char in C++?

I can specify an integer literal of type unsigned long as follows: 我可以指定unsigned long类型的整数文字,如下所示:

const unsigned long example = 9UL;

How do I do likewise for an unsigned char? 我如何为无符号字符做同样的事情?

const unsigned char example = 9U?;

This is needed to avoid compiler warning: 这是为了避免编译器警告:

unsigned char example2 = 0;
...
min(9U?, example2);

I'm hoping to avoid the verbose workaround I currently have and not have 'unsigned char' appear in the line calling min without declaring 9 in a variable on a separate line: 我希望避免我目前所拥有的冗长的解决方法,并且没有'unsigned char'出现在调用min的行中而不在单独的行中声明变量中的9:

min(static_cast<unsigned char>(9), example2);

C++11 introduced user defined literals. C ++ 11引入了用户定义的文字。 It can be used like this: 它可以像这样使用:

inline constexpr unsigned char operator "" _uchar( unsigned long long arg ) noexcept
{
    return static_cast< unsigned char >( arg );
}

unsigned char answer()
{
    return 42;
}

int main()
{
    std::cout << std::min( 42, answer() );        // Compile time error!
    std::cout << std::min( 42_uchar, answer() );  // OK
}

C provides no standard way to designate an integer constant with width less that of type int . C没有提供标准方法来指定宽度小于int类型的整数常量。

However, stdint.h does provide the UINT8_C() macro to do something that's pretty much as close to what you're looking for as you'll get in C. 但是, stdint.h确实提供了UINT8_C()宏来做一些与你正在寻找的内容非常接近的内容。

But most people just use either no suffix (to get an int constant) or a U suffix (to get an unsigned int constant). 但是大多数人只是使用无后缀(获取int常量)或U后缀(获得unsigned int常量)。 They work fine for char-sized values, and that's pretty much all you'll get from the stdint.h macro anyway. 它们适用于char大小的值,而且无论如何,这几乎都是你从stdint.h宏中得到的。

You can cast the constant. 你可以施放常数。 For example: 例如:

min(static_cast<unsigned char>(9), example2);

You can also use the constructor syntax: 您还可以使用构造函数语法:

typedef unsigned char uchar;
min(uchar(9), example2);

The typedef isn't required on all compilers. 所有编译器都不需要typedef。

If you are using Visual C++ and have no need for interoperability between compilers, you can use the ui8 suffix on a number to make it into an unsigned 8-bit constant. 如果您使用的是Visual C ++并且不需要编译器之间的互操作性,则可以在数字上使用ui8后缀,使其成为无符号的8位常量。

min(9ui8, example2);

You can't do this with actual char constants like '9' though. 你不能用像'9'这样的实际char常量来做到这一点。

Assuming that you are using std::min what you actually should do is explicitly specify what type min should be using as such 假设您正在使用std::min那么您实际应该做的是明确指定min应该使用的类型

unsigned char example2 = 0;
min<unsigned char>(9, example2);

Simply const unsigned char example = 0; 简单的const unsigned char example = 0; will do fine. 会好起来的。

我想'\\0'将是一个值为0的char字面值,但我也没有看到这一点。

There is no suffix for unsigned char types. unsigned char类型没有后缀。 Integer constants are either int or long ( signed or unsigned ) and in C99 long long . 整数常量可以是intlong (有signedunsigned ),也可以是C99 long long You can use the plain 'U' suffix without worry as long as the value is within the valid range of unsigned chars. 只要该值在无符号字符的有效范围内,您就可以毫无顾虑地使用普通的“U”后缀。

The question was how to "specify an integer 'literal' of type unsigned char in C++?". 问题是如何“在C ++中指定一个unsigned char类型的整数'文字'?”。 Not how to declare an identifier. 不是如何声明标识符。

You use the escape backslash and octal digits in apostrophes. 您在撇号中使用转义反斜杠和八进制数字。 (eg. '\\177') (例如,'\\ 177')

The octal value is always taken to be unsigned. 八进制值始终为无符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM