我有一个向量,它包含[ '6' '0' '0' '0' '0'],这是由用户输入的60,000。我需要一个整数60000,以便可以对这个数字进行操作。
我是c++和编程方面的新手。我从串口读取60,000-3,500,000的数据/数字,并且我需要一个整数,我唯一成功完成并打印的方法是使用std::vector。 我试过使用vector,但它给我奇怪的数字。
#include "SerialPort.h"
std::vector<char> rxBuf(15);
DWORD dwRead;
while (1) {
dwRead = port.Read(rxBuf.data(), static_cast<DWORD>(rxBuf.size()));
// this reads from a serial port and takes in data
// rxBuf would hold a user inputted number in this case 60,000
if (dwRead != 0) {
for (unsigned i = 0; i < dwRead; ++i) {
cout << rxBuf[i];
// this prints out what rxBuf holds
}
// I need an int = 60,000 from my vector holding [ '6' '0' '0' '0 '0']
int test = rxBuf[0 - dwRead];
cout << test;
// I tried this but it gives me the decimal equivalent of the character
numbers
}
}
我需要以一个实数的形式输出 60000,而不是作为一个向量。任何帮助将不胜感激,谢谢。
rxBuf[0 - dwRead]
有什么期望?有一个严重的误解需要纠正。你认为这是 Python 中列表切片的等价物吗? - LogicStuffvector
,改用std::string rxBuf(15, 0);
,然后使用它呢? - Ted Lyngmochar
数组没有什么不同。因此,如果您问“如何将char
数组转换为int
”,答案将是相同的。 - PaulMcKenzie