繁体   English   中英

将QString转换为十六进制?

[英]Convert QString to Hex?

我有一个QString,我从用户附加数据输入。

在QString的末尾,我需要附加“Normal”QString的十六进制表示。

例如:

QString Test("ff00112233440a0a");
QString Input("Words");

Test.append(Input);//but here is where Input needs to be the Hex representation of "Words"

//The resulting variable should be
//Test == "ff00112233440a0a576f726473";

如何从ASCII(我认为)转换为十六进制表示?

谢谢你的时间。

你非常接近:

Test.append(QString::fromLatin1(Input.toLatin1().toHex()));

解决问题的另一种方法。

给定一个字符,您可以使用以下简单函数来计算其十六进制表示。

// Call this function twice -- once with the first 4 bits and once for the last
// 4 bits of a char to get the hex representation of a char.
char toHex(char c) {
   // Assume that the input is going to be 0-F.
   if ( c <= 9 ) {
      return c + '0';
   } else {
      return c + 'A' - 10;
   }
}

您可以将其用作:

char c;
// ... Assign a value to c

// Get the hex characters for c
char h1 =  toHex(c >> 4);
char h2 = toHex(c & 0xF);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM