如:“123456”转为0x12 0x34 0x56
或者:0x12 0x34 0x56转为“123456”
//转化方向:0x12 0x34 0x56 -> "123456"
QString idStr[3];
uchar g_deviceId[3][3] = {{0x52, 0x34, 0x63}, {0x12, 0x34, 0x56}, {0x12, 0x34, 0x63}};
for(int i = 0 ; i < 3; i++) //转成连续的字符串 “123456”、“123463”、“523463”
{
for(int j = 0; j < 3; j++)
{
QString hex;
hex = QString("%1").arg(g_deviceId[i][j], 2, 16, QLatin1Char('0'));
idStr[i] += hex;
}
}
for(int i = 0; i < 3; i++)
{
qDebug() << idStr[i];
}
结果为:
//冒泡排序,从小到大 //将数字字符串排序,将较小的排在前面
for(int i = 0; i < 3; i++)
{
for(int j = i + 1; j < 3; j++)
{
if(idStr[i] > idStr[j])
{
QString temp = idStr[i];
idStr[i] = idStr[j];
idStr[j] = temp;
}
}
qDebug() << idStr[i];
}
结果为:
//转化方向:"123456" -> 0x12 0x34 0x56
for(int i = 0; i < 3; i++) //再将排好序的数字字符串两两转成16进制,存进g_deviceId
{
int index = 0;
bool ok;
for(int j = 0; j < 3; j++)
{
g_deviceId[i][j] = idStr[i].mid(index, 2).toInt(&ok, 16);//将十进制的字符串数据转成十六进制,而内容不会变(如12->0x12)
index += 2;
qDebug("%04hhX", g_deviceId[i][j]); //十六进制格式打印
}
}
结果为: