如何将 Qt 转换为 C#
How to Convert Qt to C#
我在Qt框架中使用此C++代码将十六进制转换为utf8,现在我想将其用于Visual Studio C#。
QByteArray data = QByteArray::fromHex(ui->editorText->toPlainText().toUtf8());
QString textData = QString::fromUtf8(data.constData(), data.size());
C# 中的上述代码等效于什么?
法典:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = HexToBytes(textBox1.Text);
textBox1.Text = Encoding.UTF8.GetString(bytes);
}
public static byte[] HexToBytes(string hex)
{
hex = hex.Trim();
byte[] bytes = new byte[hex.Length / 2];
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] = byte.Parse(hex.Substring(index * 2, 2), NumberStyles.HexNumber);
}
return bytes;
}
感谢@xanatos帮助我。我解决了问题,最终代码是:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = HexToBytes(textBox1.Text);
var x = Encoding.UTF8.GetString(bytes).Replace(" ", " ");
textBox1.Text = x;
}
public static byte[] HexToBytes(string hex)
{
hex = hex.Trim();
byte[] bytes = new byte[hex.Length / 2];
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] = byte.Parse(hex.Substring(index * 2, 2), NumberStyles.HexNumber);
}
return bytes;
}
相关文章:
- 将Qt Designer项目转换为C++代码
- Qt 将字符串转换为以 16 为基数的 int,但保留以 16 为基数的值
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 在Qt中将QString转换为ASCII值,反之亦然
- 如何在Windows上将Qt QString转换为LPCTSTR
- 将Qt qml文件转换为位图图像
- 将 CRC uint 转换为 QByteArray QT - C++
- QT 创建器只会将第一个单词从文本编辑转换为纯文本
- QT中不允许使用一行多个转换,例如qString到char*
- C++ QList 中项目的 QT 强制转换到子类
- 编译错误 QT - 无法从类型强制转换
- 如何将Qt文件路径从资源转换为绝对路径?
- 将时间戳转换为纳秒 (Qt)
- 将Qt的QML代码转换为C++
- 将QString转换为uintptr_t,qt / c
- 如何在Qt中将带有YUV数据的"QVideoFrame"转换为具有RGBA32数据的"QVideoframe"?
- 没有成员命名 'setRawData' in 'QwtPlotCurve' - 转换 Qt 4.7 为 Qt 5.8
- 没有成员命名 'setResizeMode' in 'QHeaderView' - 转换 Qt 4.7 为 Qt 5.8
- 添加不带发送器和接收器的转换Qt 5.2