0

これは私の問題です:

std::string str = "12 13 14 15 16.2";  // my input

私は…したい

unsigned char myChar [4]; // where myChar[0]=12 .. myChar[0]=13 ... etc...

istringstreamを使用しようとしました:

  std::istringstream is (str);
  unsigned char myChar[4];
  is >> myChar[0]  // here something like itoa is needed 
     >> myChar[1]  // does stringstream offers some mechanism 
                   //(e.g.: from char 12 to int 12) ?
     >> myChar[2]
     >> myChar[3]

しかし、私は(明らかに)得ました

myChar[0]=1 .. myChar[1]=2 .. myChar[2]=3

まさか…sprintfを使わないといけないの!??! 残念ながらboostもC++11も使えません…

ティア

4

2 に答える 2

0

私が知っている唯一の解決策は、文字列を解析することです。次に例を示します。

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
    stringstream ss("65 66 67 68 69.2");
    string value;
    int counter=0;

    while (getline(ss, value, ' '))
    {
        if (!value.empty())
        {
            cout << (unsigned char*) value.c_str() << endl;
            counter++;
        }
    }
    cout << "There are " << counter << " records." << endl;
}
于 2013-06-28T11:23:42.447 に答える