c++ 快速将原始数据转换为十六进制字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17219955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:00:40  来源:igfitidea点击:

Quickly convert raw data to hex string in c++

c++qtchar

提问by krb686

I'm reading data from a file and trying to display the raw data as 2 digit hex strings.

我正在从文件中读取数据并尝试将原始数据显示为 2 位十六进制字符串。

I'm using the Qt framework, specifically the QTextEdit.

我正在使用 Qt 框架,特别是 QTextEdit。

I've tried a bunch of different approaches and have almost accomplished what I want it to do, however it has some unexpected errors I don't know anything about.

我尝试了很多不同的方法,几乎​​完成了我想要它做的事情,但是它有一些我不知道的意外错误。

Currently this is my implementation:

目前这是我的实现:

1) Read in the data:

1)读入数据:

ifstream file (filePath, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
    size = file.tellg();
    memblock = new char [size+1];
    file.seekg(0, ios::beg);
    file.read(memblock, size);
    file.close();
}

2) Create a single QString that will be used (because QTextEdit requires a QString):

2)创建一个将被使用的 QString(因为 QTextEdit 需要一个 QString):

QString s;

3) Loop through the array appending each successive character to the QString s.

3) 遍历数组,将每个连续字符附加到 QString s。

int count = 0;
for(i=0;i<size;i++)
{
    count++;;
    s.append(QString::number(memblock[i], 16).toUpper());
    s.append("\t");
    if (count == 16)
    {
        s.append("\n");
        count -= 16;
    }
}

Now this works fine, except when it reaches a character FF, it appears as FFFFFFFFFFFFFFFF

现在这工作正常,除非当它到达一个字符时FF,它显示为FFFFFFFFFFFFFFFF

So my main questions are:

所以我的主要问题是:

  1. Why do only the 'FF' characters appear as 'FFFFFFFFFFFFFFFF' instead?
  2. Is there a way to convert the char data to base 16 strings without using QString::number?
  1. 为什么只有“FF”字符显示为“FFFFFFFFFFFFFFFF”?
  2. 有没有办法在不使用 QString::number 的情况下将字符数据转换为基数为 16 的字符串?

I want this implementation to be as fast as possible, so if something like sprintf could work, please let me know, as I would guess that might be faster that QString::number.

我希望这个实现尽可能快,所以如果像 sprintf 这样的东西可以工作,请告诉我,因为我猜这可能比 QString::number 更快。

回答by Pavel Strakhov

QStringcan't be used for binary data. You should use QByteArrayinstead. It can be easily created from char*buffer and can be easily converted to hex string using toHex.

QString不能用于二进制数据。你应该QByteArray改用。它可以很容易地从char*缓冲区创建,并且可以很容易地使用toHex.

QByteArray array(memblock, size);
textEdit->setText(QString(array.toHex()));

回答by Praetorian

QString::numberdoesn't have an overload that takes a char, so your input is being promoted to an int; consequently you're seeing the effects of sign extension. You should be seeing similar behavior for any input greater than 0x7F.

QString::number没有需要 a 的重载char,因此您的输入被提升为int; 因此,您会看到符号扩展的效果。对于大于 的任何输入,您应该会看到类似的行为0x7F

Try casting the data prior to calling the function.

在调用函数之前尝试转换数据。

s.append(QString::number(static_cast<unsigned char>(memblock[i]), 16).toUpper());