How can I convert a string in C ++ or C to an integer array?

How can I convert a string to an array of integers? Can I use sstream

it because it atoi

doesn't work ?!

-2


source to share


6 answers


As you said in the comments, you got a binary string and want to convert it to integers. To do this, use a bitset:

std::istringstream is(str);
std::bitset<32> bits; // assuming each num is 32 bits long

while(is >> bits) {
    unsigned long number = bits.to_ulong();
    // now, do whatever you want with that long.
    v.push_back(number);
}

      

If you only have one binary number on that line str

, you can get away with

unsigned long number = std::bitset<32>(str).to_ulong();

      



It is also possible to convert this to C ...

long value;
char const *c = str;
for(;;) {
    char * endp;
    value = strtol(c, &endp, 2);
    if(endp == c)
        break;

    /* huh, no vector in C. You gotta print it out maybe */
    printf("%d\n", value);
    c = endp;
}

      

atoi

cannot parse binary numbers. But he strtol

can analyze them if you tell him the right base.

+4


source


How exactly would you like to go to work? Do you just want an array containing the ASCII value of each character in the array? (so "abc" becomes [97, 98, 99, 0])?

Or are you parsing the string somehow? ("1, 2, 3" becomes array [1, 2, 3])

In the first case, in C ++, I would do something like this:



struct convert {
  int operator()(char c) {
    return static_cast<int>(c);
  }
};

std::string str = "hello world";
std::vector<int> result;
std::transform(str.begin(), str.end(), std::back_inserter(result), convert())

      

You can of course use a raw array instead of a vector, but since the length of the string is likely to be variable, then arrays just pose problems.

If this is not what you wanted, you can edit your question to be more specific.

+3


source


From what I understand, for the input string "110013" will be converted to array {1,1,0,0,1,3}. Here's how to do it in C ++:

string a = "1110011000";
vector<int> v;
for(int i = 0 ; i < a.length() ; i++){
    v.push_back(a[i] -'0');
}

// Check the result
for(int i = 0 ; i < v.size() ; i++){
    cout << v[i] << endl;
}

      

+1


source


Fast line splitter:

convert(string str, string delim, vector<int>& results)
{
  int next;
  char buf[20];
  while( (next= str.find_first_of(delim)) != str.npos ) {
    if (next> 0) 
      results.push_back(atoi(str.substr(0,next), buf, 10));
    str = str.substr(next+1);
  }
  if(str.length() > 0)
    results.push_back(atoi(str.substr(0,next), buf, 10));
}

      

You can use stringstream instead of atoi (which works, one int at a time)

int i;
stringstream s (input_string)
s >> i;

      

If you combine my code and jalf code, you end up with something really good.

0


source


Use istream_iterator in conjunction with a string stream.

By Array, I assume that you really mean std :: vector, since you don't know the number of integers at compile time. But the code can be easily modified to use an array rather than a vector.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::string     data = "5 6 7 8 9";
    std::vector<int>    store;


    std::stringstream   dataStream(data);
    std::copy(std::istream_iterator<int>(dataStream),
              std::istream_iterator<int>(),
              std::back_inserter(store)
             );

    // This line just copies the store to the std::cout
    // To verify it worked.
    std::copy(store.begin(),
              store.end(),
              std::ostream_iterator<int>(std::cout,",")
             );
}

      

0


source


Language: C

Title:

#include <stdlib.h>

      

Function prototype:

long int strtol(const char *nptr, char **endptr, int base);

      

Usage example:

strtol(nptr, (char **) NULL, 10);

      

0


source







All Articles