首页 > c++ > 将带有二进制的QString转换为带有十六进制的QString

将带有二进制的QString转换为带有十六进制的QString (Converting a QString with a binary into a QString with a hex)

2018-07-22 c++qt

问题

我想知道最有效的方法是将保存为QString的二进制文件转换为相应的Hex并将其保存在相同的QString中

QString value = "10111100" 

value = "bc"

解决方法

这很简单。首先将二进制字符串转换为整数:

QString value = "10111100";
bool fOK;
int iValue = value.toInt(&fOk, 2);  //2 is the base

然后将整数转换为十六进制字符串:

value = QString::number(iValue, 16);  //The new base is 16

问题

I am wondering what the most efficient way would be to convert a binary that is saved as a QString into the corresponding Hex and save it in the same QString

QString value = "10111100" 

into

value = "bc"

解决方法

It's simple. First convert your binary string to an integer:

QString value = "10111100";
bool fOK;
int iValue = value.toInt(&fOk, 2);  //2 is the base

Then convert the integer to hex string:

value = QString::number(iValue, 16);  //The new base is 16
相似信息