3

I have a vector with [ '6' '0' '0' '0' '0'] from a user inputting 60,000. I need an int 60000 so I can manipulate this number.

I'm new to c++ and programming in general. 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. 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.

David G
  • 94,763
  • 41
  • 167
  • 253
Chris Pineda
  • 41
  • 1
  • 4
  • Possible duplicate of [convert vector of char to an int](https://stackoverflow.com/questions/11240502/convert-vector-of-char-to-an-int) – Heath Raftery Jul 08 '19 at 21:08
  • 1
    What did you expect from `rxBuf[0 - dwRead]`? There's a serious misunderstanding that needs to be corrected. Do you think that is an equivalent to Python's list slices? – LogicStuff Jul 08 '19 at 21:09
  • What about skipping the `vector` and do `std::string rxBuf(15, 0);` and use that instead? – Ted Lyngmo Jul 08 '19 at 21:11
  • Your issue really has little to do with vector, since a vector stores its data in contiguous memory, no different than an array of `char`. Thus the answer would be the same if you asked "how do I convert an array of char to `int`" – PaulMcKenzie Jul 08 '19 at 21:30

5 Answers5

9

From this answer, you could do something like:

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

To convert your Vector to a String.

After that, you could use the std::stoi function:

int output = std::stoi(str);
    std::cout << output << "\n";
Naslausky
  • 3,443
  • 1
  • 14
  • 24
  • 2
    Note: if the vector string still is null terminated you can use `std::stoi(rxBuf.data())` directly. – DColt Jul 08 '19 at 21:13
5

Loop over the elements of an std::vector and construct an int from them:

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
Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • Not as eloquent as some other answers, but gets the job done. –  Jul 08 '19 at 21:15
  • I like this more than other answers because it does not rely on `std::string` nor `std::stoi` - no memory allocation or unnecessary checking. Although I would prefer `std::uint32_t` instead of `int`. – Quimby Jul 08 '19 at 21:24
3

Use a std::string to build your string:

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

Then use std::stoi to convert to integer; std::stoi

int i = std::stoi(istr);
Demolishun
  • 1,592
  • 12
  • 15
3

C++17 added the std::from_chars function that can do what you want without modifying or copying the input vector:

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

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
2

To minimize the need for temporary variables, use a std::string with the appropriate length as a buffer.

#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;
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108