//setup text fields
ui->lineEditAscii->setEncoding(QTextCodec::codecForName("System"));
ui->lineEditUnicode->setEncoding(QTextCodec::codecForName("UTF-16"));
void HexEditDialog::dataChangedSlot()
{
// Allows initialization of the data by calling setData() on mHexEdit.
if(!mDataInitialized)
{
QByteArray data = mHexEdit->data();
ui->lineEditAscii->setData(data);
ui->lineEditUnicode->setData(data);
ui->lineEditCodepage->setData(data);
ui->stringEditor->document()->setPlainText(lastCodec->toUnicode(data));
checkDataRepresentable(0);
case DataCWord:
{
data = "{\n" + formatLoop<unsigned short>(mData, mTypes[mIndex].itemsPerLine, [](unsigned short n)
{
return QString().sprintf("0x%04X", n);
}) + "\n};";
}
break;
case DataCDword:
{
data = "{\n" + formatLoop<unsigned int>(mData, mTypes[mIndex].itemsPerLine, [](unsigned int n)
{
return QString().sprintf("0x%08X", n);
}) + "\n};";
}
break;
case DataCQword:
{
data = "{\n" + formatLoop<unsigned long long>(mData, mTypes[mIndex].itemsPerLine, [](unsigned long long n)
{
return QString().sprintf("0x%016llX", n);
}) + "\n};";
}
break;
case DataCString:
{
data += "\"";
bool bPrevWasHex = false;
for(int i = 0; i < mData.size(); i++)
{
byte_t ch = mData.at(i);
data += printEscapedString(bPrevWasHex, ch, "\\x%02X");
}
data += "\"";
}
break;
case DataCUnicodeString: //extended ASCII + hex escaped only
{
data += "L\"";
int numwchars = mData.size() / sizeof(unsigned short);
bool bPrevWasHex = false;
for(int i = 0; i < numwchars; i++)
{
unsigned short ch = ((unsigned short*)mData.constData());
if((ch & 0xFF00) == 0) //extended ASCII
{
data += printEscapedString(bPrevWasHex, ch, "\\x%04X");
}
else //full unicode character
{
bPrevWasHex = true;
data += QString().sprintf("\\x%04X", ch);
}
}
data += "\"";
}
break;
case DataCShellcodeString:
{
data += "\"";
for(int i = 0; i < mData.size(); i++)
{
byte_t ch = mData.at(i);
data += QString().sprintf("\\x%02X", ch);
}
data += "\"";
}
break;
case DataString:
{
data = QTextCodec::codecForName("System")->makeDecoder()->toUnicode((const char*)(mData.constData()), mData.size());
}
break;
case DataUnicodeString:
{
data = QString::fromUtf16((const ushort*)(mData.constData()), mData.size() / 2);
}
break;
case DataUTF8String:
{
data = QString::fromUtf8((const char*)mData.constData(), mData.size());
}
break;
case DataUCS4String:
{
data = QString::fromUcs4((const uint*)(mData.constData()), mData.size() / 4);
}
break;
case DataASMByte:
{
data = "array DB " + formatLoop<unsigned char>(mData, mTypes[mIndex].itemsPerLine, [](unsigned char n)
{
QString value = QString().sprintf("%02Xh", n);
if(value.at(0).isLetter())
value.insert(0, '0');
return value;
});
}
break;
case DataASMWord:
{
data = "array DW " + formatLoop<unsigned short>(mData, mTypes[mIndex].itemsPerLine, [](unsigned short n)
{
QString value = QString().sprintf("%04Xh", n);
if(value.at(0).isLetter())
value.insert(0, '0');
return value;
});
}
break;
case DataASMDWord:
{
data = "array DD " + formatLoop<unsigned int>(mData, mTypes[mIndex].itemsPerLine, [](unsigned int n)
{
QString value = QString().sprintf("%08Xh", n);
if(value.at(0).isLetter())
value.insert(0, '0');
return value;
});
}
break;
case DataASMQWord:
{
data = "array DQ " + formatLoop<unsigned long long>(mData, mTypes[mIndex].itemsPerLine, [](unsigned long long n)
{
QString value = QString().sprintf("%016llXh", n);
if(value.at(0).isLetter())
value.insert(0, '0');
return value;
});
}
break;
case DataASMString:
{
QString line;
int index = 0;
bool bPrevWasHex = false;
while(index < mData.size())
{
QChar chr = QChar(mData.at(index));
if(chr >= ' ' && chr <= '~')
{
if(line.length() == 0)
line += "\"";
if(bPrevWasHex)
{
line += ",\"";
bPrevWasHex = false;
}
if(line.length() == 0)
line += asmhex;
else if(!bPrevWasHex)
line += "\"," + asmhex;
else
line += "," + asmhex;
bPrevWasHex = true;
}
index++;
}
if(!bPrevWasHex)
line += "\"";
data = line;
}
break;
case DataPascalByte:
{
data += QString().sprintf("Array [1..%u] of Byte = (\n", mData.size());
data += formatLoop<unsigned char>(mData, mTypes[mIndex].itemsPerLine, [](unsigned char n)
{
return QString().sprintf("$%02X", n);
});
data += "\n);";
}
break;
case DataPascalWord:
{
data += QString().sprintf("Array [1..%u] of Word = (\n", mData.size() / 2);
data += formatLoop<unsigned short>(mData, mTypes[mIndex].itemsPerLine, [](unsigned short n)
{
return QString().sprintf("$%04X", n);
});
data += "\n);";
}
break;
case DataPascalDword:
{
data += QString().sprintf("Array [1..%u] of Dword = (\n", mData.size() / 4);
data += formatLoop<unsigned int>(mData, mTypes[mIndex].itemsPerLine, [](unsigned int n)
{
return QString().sprintf("$%08X", n);
});
data += "\n);";
}
break;
case DataPascalQword:
{
data += QString().sprintf("Array [1..%u] of Int64 = (\n", mData.size() / 8);
data += formatLoop<unsigned long long>(mData, mTypes[mIndex].itemsPerLine, [](unsigned long long n)
{
return QString().sprintf("$%016llX", n);
});
data += "\n);";
}
break;
case DataPython3Byte:
{
data += "b\"";
for(int i = 0; i < mData.size(); i++)
{
byte_t ch = mData.at(i);
data += QString().sprintf("\\x%02X", ch);
}
data += "\"";
}
break;
case DataHexStream:
{
for(int i = 0; i < mData.size(); i++)
{
byte_t ch = mData.at(i);
data += QString().sprintf("%02X", ch);
}
}
break;
void HexEditDialog::dataChangedSlot()
{
// Allows initialization of the data by calling setData() on mHexEdit.
if(!mDataInitialized)
{
QByteArray data = mHexEdit->data();
// ... 省略后续 ...
}
}
其中 QByteArray data = mHexEdit->data(); 应该是你需要的,从 QHexEdit 控件获取字节码。