【问题标题】:How to convert from vector<char> to a number integer如何从vector<char>转换为数字整数
【发布时间】:2019-11-18 10:00:45
【问题描述】:

我有一个来自用户输入 60,000 的带有 [ '6' '0' '0' '0' '0'] 的向量。我需要一个 int 60000 以便我可以操纵这个数字。

我是 C++ 和一般编程的新手。我从串行端口读取 60,000-3,500,000 的数据/数字,我需要一个整数,我成功完成并打印它的唯一方法是通过 std::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 的列表切片吗?
  • 跳过vector 并改用std::string rxBuf(15, 0); 怎么样?
  • 您的问题实际上与向量无关,因为向量将其数据存储在连续内存中,与char 的数组没有什么不同。因此,如果您问“如何将 char 数组转换为 int”,答案将是相同的

标签: c++ visual-c++ char type-conversion stdvector


【解决方案1】:

循环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

【讨论】:

  • 不像其他一些答案那样雄辩,但可以完成工作。
  • 我比其他答案更喜欢这个,因为它不依赖 std::string 也不依赖 std::stoi - 没有内存分配或不必要的检查。虽然我更喜欢std::uint32_t 而不是int
【解决方案2】:

从这个answer,您可以执行以下操作:

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

将您的向量转换为字符串。

之后,您可以使用std::stoi 函数:

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

【讨论】:

  • 注意:如果向量字符串仍然是空终止的,您可以直接使用std::stoi(rxBuf.data())
【解决方案3】:

使用 std::string 来构建你的字符串:

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

然后使用 std::stoi 转换为整数; std::stoi

int i = std::stoi(istr);

【讨论】:

    【解决方案4】:

    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

    【讨论】:

      【解决方案5】:

      为尽量减少对临时变量的需求,请使用具有适当长度的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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-02
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2014-05-16
        • 2019-03-08
        • 1970-01-01
        相关资源
        最近更新 更多