Qt 十六进制与浮点数相互转换

https://blog.csdn.net/XXYBMOOO/article/details/131569944

https://lostphp.com/index/tool/hexconvert

float 转 16

QString str = "ABCDE";
float f = str .toFloat();
int i = *((int *)&f);
QString str_float = QString("%1").arg(i, 4, 16, QLatin1Char('0'));
qDebug() << "str_float: " << str_float ;

16进制字符串 转 float

QString strHex = "0x2222";
int c = strHex.toInt(nullptr, 16);
float d = (float)&c;
QString str= QString("%1").arg(d);
qDebug() << "str: " << str;

16进制字符串 转 float

1:QString 转 char*
void MainWindow::QString2Hex(QString src,char* charresult)
{
bool ok;
uint Bytesize = src.size()/2;
for (int j =0;j<Bytesize;++j )
{
QString hexstring = ("0X"+src.mid(j*2,2));
charresult[j] = hexstring.toUInt(&ok,16);
}
}

char * str1;
float a;
QString2Hex(str,str1);
memcpy(&a, str1, sizeof(float));
qDebug()<<a;

16进制字符串 转 float

IEE754,需注意大端倒序

16进制转float

//0077823e
unsigned char byte_arr[] = {0xc3, 0xaf, 0x78, 0xd5};  
float a;
memcpy(&a, byte_arr, sizeof(float));

TRACE("a = %4.3f \n",a);
posted @ 2024-10-25 11:59  mkmkbj  阅读(14)  评论(0编辑  收藏  举报