简体   繁体   English

将逗号分隔的十六进制(字符串)数组转换为整数或浮点型

[英]Convert Comma Separated Hex (String) Array to either integer or float

The array is set up like so: 数组设置如下:

string * str = new string[11];

Where the content of the string looks like: 字符串的内容如下所示:

str[0]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
str[1]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
str[2]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
...
str[12]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK

Another array looks like: 另一个数组如下所示:

string * type = new string[11];

Where the content is: 内容在哪里:

type[0]="1";
type[1]="1";
type[2]="1";
type[3]="1";
type[4]="2";
type[5]="1";
type[6]="1";
type[7]="2";
type[8]="2";
type[9]="2";
type[10]="2";

These types correspond to each value in the string, so, for the first string: 这些类型对应于字符串中的每个值,因此对于第一个字符串:

1=float , 2=integer 1 =浮点数,2 =整数

  • AAAAAAAA would be 1 ; AAAAAAAA将为1 ; or an float 或浮法
  • BBBBBBBB would be 1 ; BBBBBBBB将为1 ; or an float 或浮法
  • CCCCCCCC would be 1 ; CCCCCCCC为1 ; or an float 或浮法
  • DDDDDDDD would be 1 ; DDDDDDDD将为1 ; or an float 或浮法
  • EEEE would be 2 ; EEEE为2 ; or a integer 或整数
  • FFFFFFFF would be 1 ; FFFFFFFF为1 ; or an float 或浮法
  • GGGGGGGG would be 1 ; GGGGGGGG为1 ; or an float 或浮法
  • HHHH would be 2 ; HHHH将为2 ; or a integer 或整数
  • IIII would be 2 ; IIII为2 ; or a integer 或整数
  • JJJJ would be 2 ; JJJJ将为2 ; or a integer 或整数
  • KKKK would be 2 ; KKKK为2 ; or a integer 或整数

In addition the single type array works for all strings in the str array. 另外,单一类型数组适用于str数组中的所有字符串。

Now for my question: How do i use the above information to extract each individual values from the string and convert it to an integer or a float based on the value in the type array. 现在是我的问题:我如何使用以上信息从字符串中提取每个单独的值,然后根据类型数组中的值将其转换为整数或浮点数。

BE AWARE: Boost is not available to me 注意:Boost对我不可用

The conversion functions look like: (The other is formatted similarly except for an integer) 转换函数看起来像:(除整数外,其他函数的格式类似)

unsigned int BinaryParser::hexToFloat(std::string hexInput)
{   
    std::stringstream ss (hexInput);
    unsigned int floatOutput;
    ss >> hex >> floatOutput;
    return reinterpret_cast<float&>(floatOutput);
}

OK, first part: extract the comma-separated strings. 好,第一部分:提取逗号分隔的字符串。 One way would be: 一种方法是:

std::vector<std::string> split( std::string s ){
  std::vector<std::string> vec;
  int pos = 0;
  while( std::string::npos != (pos = s.find( ',', pos ) ) ){
    vec.push_back( s.substr( 0, pos ) );
    s = s.substr( pos + 1 );
  }
  vec.push_back( s );
  return vec;
}

Depends on the input string being "well-behaved". 取决于输入字符串是否“行为良好”。

This converts an int from hex digits: 这将从十六进制数字转换为整数:

int convInt( std::string hexInput ){
  std::istringstream iss (hexInput);
  uint16_t intOutput;
  iss >> std::hex >> intOutput;
  return intOutput;
}

Float cannot be read using std::hex, so we assume the HHHHHHHH is a float's bytes interpreted as an int32_t. 无法使用std :: hex读取浮点数,因此我们假设HHHHHHHH是浮点数的字节,被解释为int32_t。

float convFloat( std::string & hexInput ){
  std::istringstream iss (hexInput);
  uint32_t intOutput;
  iss >> std::hex >> intOutput;
  return reinterpret_cast<float&>(intOutput);
}

For storing the results we can use: 为了存储结果,我们可以使用:

enum TypeTag { eInt, eFloat };

class IntOrFloat {
public:
  IntOrFloat( int i ) : typeTag(eInt),integer(i),floating(0) { }
  IntOrFloat( float f ) : typeTag(eFloat),integer(0),floating(f) { }
  virtual ~IntOrFloat(){}
  int getInt() const { return integer; }
  float getFloat() const { return floating; }
  TypeTag getTypeTag() const { return typeTag; }
private:
  TypeTag typeTag;
  int integer;
  float floating;
};


std::ostream& operator<< (std::ostream& os, const IntOrFloat& iof){
  switch( iof.getTypeTag() ){
  case eInt:
    os << iof.getInt();
    break;
  case eFloat:
    os << iof.getFloat();
    break;
  }
  return os;
}

To convert one comma-separated string according to the type vector: 根据类型向量转换一个逗号分隔的字符串:

std::vector<IntOrFloat> convert( const std::vector<std::string> t, const std::string s ){
  std::vector<IntOrFloat> results;
  std::vector<std::string> hexes = split( s );
  for( int i = 0; i < hexes.size(); i++ ){
    if( t[i] == "1" ){
      results.push_back( IntOrFloat( convFloat( hexes[i] ) ) );
    } else {
      results.push_back( IntOrFloat( convInt( hexes[i] ) ) );
    }
  }
  return results;
}

That's it, then. 就是这样。 - I've been using vector instead of the arrays. -我一直在使用向量而不是数组。 You can easily convert, eg 您可以轻松地进行转换,例如

std::vector<std::string> fromArray( std::string strs[], int n ){
  std::vector<std::string> strings;
  for( int i = 0; i < n; i++ ) strings.push_back( std::string( strs[i] ) );
  return strings;
}
#define fromArray(a) fromArray( a, (sizeof(a)/sizeof(a[0])) )

And here is my test program: 这是我的测试程序:

#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
int main(){
  std::string t[] = {"2","1","1","2"};
  std::string s[] = {
    "8000,4048f5c3,bf000000,FFFF",
    "0001,42f6e979,c44271ba,7FFF",
    "1234,00000000,447a0000,5678"
  };

  std::vector<std::string> types = fromArray( t );
  std::vector<std::string> strings  = fromArray( s );
  for( std::vector<std::string>::iterator it = strings.begin() ; it != strings.end(); ++it ){
    std::vector<IntOrFloat> results = convert( types, *it );
    std::cout << "converting string " << *it << ", " << results.size() << " values:" <<   std::endl;
    for( std::vector<IntOrFloat>::iterator iof = results.begin() ; iof != results.end(); ++iof ){
      std::cout << "  " << *iof << std::endl;
    }
  }
}

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

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