简体   繁体   English

1字节无符号整数c ++

[英]1 byte unsigned integer c++

I have programmed a class called HugeInteger which can do arithmetic (add, sub, multiply) with numbers of "infinitely" size. 我编写了一个名为HugeInteger的类,它可以使用“无限”大小的数字进行算术运算(加,子,乘)。 It treats each bit of the digit in the number as a stand-alone digit (eg 1234 = 1, 2, 3 and 4). 它将数字中的每位数字视为独立数字(例如1234 = 1,2,3和4)。 I store these numbers in a vector (vector<short>) . 我将这些数字存储在矢量(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. 现在,因为每个数字只能取0到9之间的值,所以我真的不需要将它们存储为2字节数字。 Is there a way (without using char) to store the digits as a 1 byte unsigned integer? 有没有办法(不使用char)将数字存储为1字节无符号整数? Thanks! 谢谢!

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? 我应该使用哪种数据类型来迭代向量?

Yes, use unsigned char . 是的,使用unsigned char

If <stdint.h> is available, then you could also use uint8_t . 如果<stdint.h>可用,那么您也可以使用uint8_t

Don't let the standard compiler type char confuse you; 不要让标准的编译器类型char混淆你; the following is perfectly legal: 以下是完全合法的:

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; 并不是说char类型有什么特别的东西会迫使你把它们想象成字符; 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. 相反,它只是1个字节的方便大小,这使得它们适合保存诸如printf库例程使用它们来保存1字节值的值,它将在合适的编码下解释为字符。

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. 如果您的系统支持大小为1字节的数据类型,那么就是这样。 Otherwise it will be the next smallest data type available. 否则它将是可用的下一个最小数据类型。

You may need to cast the value when using a stream insertion operator to make sure you get numeric output instead of character treatment. 您可能需要在使用流插入运算符时强制转换值,以确保获得数字输出而不是字符处理。

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) 你在无符号字符上使用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) unsigned char的底层表示仍然是你推入向量的数字 - unsigned char仍然是无符号的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] );

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. 使用char或unsigned char作为1字节整数类型并不总是那么简单...有时您只需要类型为数字类型,而不是字符类型。 One such example is here: 1 byte integer data type Other is when you have function overloaded for arguments of several different types. 这里有一个这样的例子: 1字节整数数据类型其他是当你为几种不同类型的参数重载函数时。

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

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