gpt4 book ai didi

c++ - 将逗号分隔的十六进制(字符串)数组转换为整数或 float

转载 作者:行者123 更新时间:2023-11-28 07:08:52 28 4
gpt4 key购买 nike

数组是这样设置的:

string * str = new string[11];

字符串的内容如下:

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

另一个数组看起来像:

string * type = new string[11];

内容在哪里:

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";

这些类型对应于字符串中的每个值,因此,对于第一个字符串:

1= float ,2=整数

  • AAAAAAAA 将是 1;或 float
  • BBBBBBBB 将是 1;或花车
  • CCCCCCCC 将为 1;或 float
  • DDDDDDDD 将是 1;或花车
  • EEEE 为 2;或整数
  • FFFFFFFF 将是 1;或花车
  • GGGGGGGG 将是 1;或花车
  • HHHH 将是 2;或整数
  • IIII 将是 2;或整数
  • JJJJ 将是 2;或整数
  • KKKK 将是 2;或整数

此外,单一类型数组适用于 str 数组中的所有字符串。

现在我的问题是:我如何使用上述信息从字符串中提取每个单独的值,并根据类型数组中的值将其转换为整数或 float 。

注意:我无法使用 Boost

转换函数看起来像:(另一个格式类似,除了一个整数)

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

最佳答案

好的,第一部分:提取逗号分隔的字符串。一种方法是:

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;
}

取决于输入字符串是否“行为良好”。

从十六进制数字转换为 int:

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

无法使用 std::hex 读取 float ,因此我们假设 HHHHHHHH 是一个 float 的字节,被解释为 int32_t。

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

为了存储我们可以使用的结果:

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;
}

根据类型 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;
}

那就这样吧。 - 我一直在使用 vector 而不是数组。您可以轻松转换,例如

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])) )

这是我的测试程序:

#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;
}
}
}

关于c++ - 将逗号分隔的十六进制(字符串)数组转换为整数或 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335703/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com