- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
数组是这样设置的:
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=整数
此外,单一类型数组适用于 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/
我有一个字符串 "1,23,45,448.00",我想用小数点替换所有逗号,用逗号替换所有小数点。 我需要的输出是“1.23.45.448,00” 我尝试用 . 替换 , 如下: var mystri
我刚刚开始阅读 C。 情况: x = ( y = 3, ( z = ++y + 2 ) + 5 ) 它首先评估y = 3,然后评估( z =++y + 2) 我的问题: 它应该首先评估( z =++y
我是 VIM 初学者,我有一个奇怪的问题。我开始在终端模拟器中使用 vim,但今天我转向了 gVim。然后我意识到我不能在插入模式下写逗号!我试过 :map , , :imap ,都说没有找到映射。然
我上面有一个 JavaScript 正则表达式函数代码: function AcceptDigits(objtextbox) { var exp = /[^\d{1,3}]/g; ob
我需要使用 Java 的 split() 方法分割字符串。如何编写某个单词的分隔符的正则表达式模式?例如“和”? 我得到了分割空格和逗号的模式,即 [,\\s] 但我想添加单词 and ,以便它也成为
我们通常使用逗号来强调句子中的某些内容。但是说到搜索引擎优化,我只是想知道这是否有用,尤其是当标题标签或页面标题中所需的字符数有限(65到70个)以便爬虫读取时。我们是否需要使用逗号? 最佳答案 页面
我想找出每个学科学期的学生上课人数。以下是给出正确答案的查询 select semester,subject,count(stdId) from tblSubjectMaster group by s
int m = 5, d = 12, y = 1975, val; // May 12, 1975 谁能解释一下下面代码行中逗号运算符的功能/用途: val = (d+=m<3?y--:y-2
我不能在 Visual Studio 2013 中使用 Ctrl+, 和在 Windows 8.1 中使用 Pycharm。有没有办法找到哪个应用程序占用了这个快捷方式/热键? 我试过 Windows
我发现很难用谷歌搜索 powershell 中这两种执行方法调用的区别的答案。 : $member = "1.2.3.4:567" # IPaddress + port for demonstrati
谁能建议我如何在 emacs 中通过 n 逗号前进(或后退)? 我正在尝试浏览 CSV 文件 我知道我可以做类似的事情: C-u 100 M-f 但是能够在逗号上做更多的正则表达式会更准确 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这是一个棘手的问题,我会尽力描述它。 我有一个文本输入,用户可以在标签中输入。 用户在输入中键入标签(例如“starcraft”)并按下逗号键后,我希望逗号之前的所有字符(即单词“starcraft”
我如何打印 csv 文件中逗号后的最后一个值,我想循环遍历该文件并打印出最后一个值: 数据: 20181016135716, 00-00-00-00-00-04, 10.0.0.1, 10.0.0.8
这个问题在这里已经有了答案: How to format the HH:mm:ss separators of a TimeSpan in a culture-aware manner? (3 个答
我想去掉除数字、$、逗号 (,) 之外的所有内容。 这只是 strip 字母 string Cadena; Cadena = tbpatronpos6.Text;
我已经在我的 xml 文件中创建了一个编辑文本。这是我的代码: 问题是在我的输出项目中它只能工作其中一个。只有点或只有逗号。怎么了 ?在模拟器中工作正常,只有在我的版本中没有。 最佳答案 我用不同
我正在尝试编写一个正则表达式来提取由空格分隔的单词(可选逗号 + 空格),从单词中删除“堆栈”前缀(如果有的话)。我正在尝试找到一个纯正则表达式解决方案,而无需对结果进行任何后处理或类似(如果可能)。
我正在尝试在数据库中插入一些数据,但遇到了一些麻烦。目前我无法将数据添加到我的表中,我不知道为什么。 我正在使用一些您不需要理解的通用方法,这适用于我的所有项目,并且在任何项目中都没有任何错误。 以下
我想从 json 中删除尾随逗号, { "key1": "value1", "object": { "key2": "value2", // <- remove comma },
我是一名优秀的程序员,十分优秀!