I using this C++ code in Qt framework for convert hex to utf8, Now I want to use this into Visual Studio C#.
QByteArray data = QByteArray::fromHex(ui->editorText->toPlainText().toUtf8());
QString textData = QString::fromUtf8(data.constData(), data.size());
What is equivalent of above code in C#?
Code:
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;
}