1 字节无符号整数 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9966663/
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
1 byte unsigned integer c++
提问by raze
I have programmed a class called HugeInteger which can do arithmetic (add, sub, multiply) with numbers of "infinitely" size. It treats each bit of the digit in the number as a stand-alone digit (e.g. 1234 = 1, 2, 3 and 4). I store these numbers in a vector (vector<short>)
. Now, because each digit only can take the values from 0 to 9, i don't really need to store them as a 2 byte digit. Is there a way (without using char) to store the digits as a 1 byte unsigned integer? Thanks!
我编写了一个名为 HugeInteger 的类,它可以用“无限”大小的数字进行算术运算(加、减、乘)。它将数字中的每一位视为一个独立的数字(例如 1234 = 1、2、3 和 4)。我将这些数字存储在一个 vector 中(vector<short>)
。现在,因为每个数字只能取 0 到 9 的值,所以我真的不需要将它们存储为 2 字节数字。有没有办法(不使用字符)将数字存储为 1 字节无符号整数?谢谢!
Update:
更新:
vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
for (size_t i = 0; i < v.size(); i++)
cout << v[i];
This produces an unwanted output. Which datatype should I use to iterate through the vector?
这会产生不需要的输出。我应该使用哪种数据类型来遍历向量?
回答by Oliver Charlesworth
Yes, use unsigned char
.
是的,使用unsigned char
.
If <stdint.h>
is available, then you could also use uint8_t
.
如果<stdint.h>
可用,那么您也可以使用uint8_t
.
回答by chepner
Don't let the standard compiler type char
confuse you; the following is perfectly legal:
不要让标准编译器类型char
迷惑你;以下是完全合法的:
char number[5] = { 1, 4, 3, 6, 2}; // Representation of decimal 14,362
It's not that there is anything special about the char
type that forces you to think of them as characters; rather, it's is their convenient size of just 1 byte that makes them suitable to hold values that library routines such as printf
use them to hold the 1-byte values that it will interpret as characters under a suitable encoding.
并不是说这种char
类型有什么特别之处迫使您将它们视为字符;相反,正是它们方便的大小仅为 1 个字节,这使它们适合保存库例程的值,例如printf
使用它们保存 1 个字节的值,这些值将在合适的编码下解释为字符。
回答by Ben Voigt
uint_least8_t
is the most compact data type for storing a single decimal digit.
uint_least8_t
是用于存储单个十进制数字的最紧凑的数据类型。
If your system supports a data type of size 1 byte, this will be it. Otherwise it will be the next smallest data type available.
如果您的系统支持大小为 1 字节的数据类型,那就是它。否则,它将是下一个可用的最小数据类型。
You may need to cast the value when using a stream insertion operator to make sure you get numeric output instead of character treatment.
在使用流插入运算符时,您可能需要转换该值,以确保获得数字输出而不是字符处理。
回答by Ben Cottrell
The output you're seeing from using cout << on an unsigned char is down to the mechanics of the << operator when used with a std::ostream (specifically, different overloads of the operator << will display the value in different ways - the char/unsigned char overloads usually assume that you want a character representation instead of a numeric one)
您在 unsigned char 上使用 cout << 所看到的输出归结为与 std::ostream 一起使用时 << 运算符的机制(具体而言,运算符 << 的不同重载将以不同方式显示值- char/unsigned char 重载通常假设您需要字符表示而不是数字表示)
The underlying representation of your unsigned char is still the same number which you pushed into the vector - an unsigned char is still an unsigned 1-byte integer)
您的无符号字符的底层表示仍然与您推入向量的数字相同 - 无符号字符仍然是无符号的 1 字节整数)
If you wish to change the output, then you need to avoid using the overload of the << operator which is designed for char or unsigned char - the easiest way to do that is to perform a cast
如果您希望更改输出,则需要避免使用为 char 或 unsigned char 设计的 << 运算符的重载 - 最简单的方法是执行强制转换
vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
for (size_t i = 0; i < v.size(); i++)
cout << static_cast<int>( v[i] );
回答by Telvas
Using char or unsigned char as 1 byte integer type is not always that straightforward... Sometimes you just need the type to be number type, not character type. One such example is here: 1 byte integer data typeOther is when you have function overloaded for arguments of several different types.
使用 char 或 unsigned char 作为 1 字节整数类型并不总是那么简单......有时你只需要类型是数字类型,而不是字符类型。一个这样的例子在这里:1 字节整数数据类型其他是当您为几种不同类型的参数重载函数时。