-3

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;
        }
  • @user3082018 why don't you break the problem down into parts and find a solution yourself? To convert Hex->String, first find a way to convert ASCII hex into bytes. Then find a way to convert the bytes into Unicode by decoding as UTF-8. For String->Hex, reverse the process? – Alastair McCormack Mar 14 '16 at 12:27
  • See `HexToBytes()` here: http://www.java2s.com/Code/CSharp/Data-Types/HexToUnicode.htm. Then perhaps you could share you answer when you've finished – Alastair McCormack Mar 14 '16 at 12:29
  • Also, rather than ask the question as a comparison to C++, just ask "How do I convert ASCII hex encoding in UTF-8 to Strings in C# and Visa-versa?" – Alastair McCormack Mar 14 '16 at 12:31
  • 1
    @AlastairMcCormack tahnks. http://www.java2s.com/Code/CSharp/Data-Types/HexToUnicode.htm isn't for my problem. I checked these samples. – user3082018 Mar 14 '16 at 12:46
  • It's part of the solution - you might have to think for yourself on this one. `HexToBytes()` deals with converting your String of hex into bytes. Next, `Encoding.UTF8.GetString(bytes);` – Alastair McCormack Mar 14 '16 at 12:52
  • 1
    @user3082018 I can tell you that that viewer is stripping zero bytes... You can see it if you try writing `310031`... You'll get `11` instead of getting `1(something)1`. – xanatos Mar 14 '16 at 13:04
  • 1
    I tested that and result was "\�Ʉ". Really that's correc?! – user3082018 Mar 14 '16 at 13:06
  • 1
    @AlastairMcCormack check sample in my edited question. thanks – user3082018 Mar 14 '16 at 13:07
  • 1
    @xanatos please give me code. – user3082018 Mar 14 '16 at 13:11
  • 1
    @AlastairMcCormack please check edited question and attachecd picture of result. – user3082018 Mar 14 '16 at 13:32
  • 3
    @user3082018 The code is correct. It is the hex data that is wrong. That data doesn't have any sense. But if you really want to have it, simply do: `Encoding.UTF8.GetString(bytes).Replace("\0", "(null)")`. This will strip null bytes that stop the textbox from showing the full string. – xanatos Mar 14 '16 at 13:36

1 Answers1

1

Thanks from @xanatos for help me. I solved problem and final code is:

private void button1_Click(object sender, EventArgs e)
    {
        byte[] bytes = HexToBytes(textBox1.Text);
        var x = Encoding.UTF8.GetString(bytes).Replace("\0", " ");
        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;
    }