如何将 Qt 转换为 C#

How to Convert Qt to C#

本文关键字:转换 Qt      更新时间:2023-10-16

我在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;
    }