相关疑难解决方法(0)

在C ++中将带有前导“ 0x”的十六进制字符串转换为带符号的缩写?

我找到了将十六进制字符串转换为signed intusing 的代码strtol,但找不到短整数(2个字节)的内容。这是我的代码:

while (!sCurrentFile.eof() )
{
    getline (sCurrentFile,currentString);
    sOutputFile<<strtol(currentString.c_str(),NULL,16)<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我的想法是读取一个具有2个字节宽的值的文件(如0xFFEE),将其转换为带符号的int并将结果写入输出文件中。执行速度不是问题。

我可以找到一些避免该问题的方法,但是我想使用“单行”解决方案,所以也许您可以为此提供帮助:)

编辑:文件看起来像这样:

while (!sCurrentFile.eof() )
{
    getline (sCurrentFile,currentString);
    sOutputFile<<strtol(currentString.c_str(),NULL,16)<<endl;
}
Run Code Online (Sandbox Code Playgroud)

编辑:我已经尝试了十六进制运算符,但这样做之前我仍然必须将字符串转换为整数。

// This won't work as currentString is not an integer
myInt << std::hex << currentString.c_str(); 
Run Code Online (Sandbox Code Playgroud)

c++ string int hex short

2
推荐指数
1
解决办法
5259
查看次数

标签 统计

c++ ×1

hex ×1

int ×1

short ×1

string ×1