简体   繁体   English

如何从矢量转换 <char> 到一个整数

[英]How to convert from vector<char> to a number integer

I have a vector with [ '6' '0' '0' '0' '0'] from a user inputting 60,000. 我有一个带有['6''''0''0''0']的向量来自输入60,000的用户。 I need an int 60000 so I can manipulate this number. 我需要一个int 60000,所以我可以操纵这个数字。

I'm new to c++ and programming in general. 我是c ++和编程的新手。 I read data/numbers from 60,000-3,500,000 from a serial port and I need an integer number, the only way I have successfully done this and printed it is through std::vector. 我从一个串口读取60,000-3,500,000的数据/数字,我需要一个整数,我成功完成这个并打印它的唯一方法是通过std :: vector。 I tried to do vector but it gives me funky numbers. 我试着做矢量,但它给了我时髦的数字。

#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
  }
}

I need an output of 60000 not in a vector but rather as a solid number, any help would be appreciated thanks. 我需要60000的输出不在矢量中而是作为一个实数,任何帮助将不胜感激谢谢。

From this answer , you could do something like: 从这个答案 ,你可以做一些事情:

std::string str(rxBuf.begin(), rxBuf.end());

To convert your Vector to a String. 将Vector转换为String。

After that, you could use the std::stoi function: 之后,您可以使用std :: stoi函数:

int output = std::stoi(str);
    std::cout << output << "\n";

Loop over the elements of an std::vector and construct an int from them: 循环遍历std::vector的元素并从中构造一个int

std::vector<char> chars = {'6', '0', '0', '0', '0'};

int number = 0;

for (char c : chars) {
    number *= 10;
    int to_int = c - '0'; // convert character number to its numeric representation
    number += to_int;
}

std::cout << number / 2; // prints 30000

Use a std::string to build your string: 使用std :: string来构建字符串:

std::string istr;
char c = 'o';
istr.push_back(c);

Then use std::stoi to convert to integer; 然后使用std :: stoi转换为整数; std::stoi 的std :: Stoi旅馆

int i = std::stoi(istr);

C++17 added the std::from_chars function that can do what you want without modifying or copying the input vector: C ++ 17添加了std::from_chars函数,它可以执行您想要的操作而无需修改或复制输入向量:

std::vector<char> chars = {'6', '0', '0', '0', '0'};
int number;
auto [p, ec] = std::from_chars(chars.data(), chars.data() + chars.size(), number);
if (ec != std::errc{}) {
    std::cerr << "unable to parse number\n";
} else {
    std::cout << "number is " << number << '\n';
}

Live Demo 现场演示

To minimize the need for temporary variables, use a std::string with the appropriate length as a buffer. 要最小化对临时变量的需求,请使用具有适当长度的std::string作为缓冲区。

#include "SerialPort.h"
#include <string>

std::string rxBuf(15, '\0');
DWORD dwRead;

while (1) {
    dwRead = port.Read(rxBuf.data(), static_cast<DWORD>(rxBuf.size()));

    if (dwRead != 0) {
        rxBuf[dwRead] = '\0'; // set null terminator
        int test = std::stoi(rxBuf);
        cout << test;
    }
}

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

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