【问题标题】:Convert hex-encoded string to unicode text将十六进制编码的字符串转换为 unicode 文本
【发布时间】:2011-08-29 19:46:35
【问题描述】:

我有像“74657374696e67”(即“测试”)这样的字符串,十六进制编码的 unicode 文本。需要将其转换回可读输出。 如何在 .NET 中执行此操作?

更新:

文本最初是使用以下 Visual Basic 6 函数编码的:

Public Function EnHex(Data As String) As String
    Dim iCount As Double, sTemp As String
    Reset
    For iCount = 1 To Len(Data)
        sTemp = Hex$(Asc(Mid$(Data, iCount, 1)))
        If Len(sTemp) < 2 Then sTemp = "0" & sTemp
        Append sTemp
    Next
    EnHex = GData
    Reset
End Function

解码如下:

Public Function DeHex(Data As String) As String
    Dim iCount As Double
    Reset
    For iCount = 1 To Len(Data) Step 2
        Append Chr$(Val("&H" & Mid$(Data, iCount, 2)))
    Next
    DeHex = GData
    Reset
End Function

【问题讨论】:

  • 十六进制数字表示字节,但 Unicode 文本最初是如何编码为字节的呢?有不止一种选择。
  • 您引用的编码函数会误用高于 255 的 Unicode 代码点,因此您可以有效地假设您的十六进制字符串描述的是 Latin-1 文本而不是 Unicode。

标签: .net string hex


【解决方案1】:

有趣的问题。

google了一下,我在VB.NET中找到了这个

Function FromHex(ByVal Text As String) As String

  If Text Is Nothing OrElse Text.Length = 0 Then
    Return String.Empty
  End If

  Dim Bytes As New List(Of Byte)
  For Index As Integer = 0 To Text.Length - 1 Step 2
    Bytes.Add(Convert.ToByte(Text.Substring(Index, 2), 16))
  Next

  Dim E As System.Text.Encoding = System.Text.Encoding.Unicode
  Return E.GetString(Bytes.ToArray)

End Function

【讨论】:

  • 此代码由于某种原因无法运行。 ArgumentOutOfRangeException, "索引和长度必须引用字符串中的位置。"
【解决方案2】:
var myString = System.Text.Encoding.UTF8.GetString(DecodeHexString("74657374696e67"));

public static byte[] DecodeHexString(string str)
{
    uint num = (uint) (str.Length / 2);
    byte[] buffer = new byte[num];
    int num2 = 0;
    for (int i = 0; i < num; i++)
    {
        buffer[i] = (byte) ((HexToByte(str[num2]) << 4) | HexToByte(str[num2 + 1]));
        num2 += 2;
    }
    return buffer;
}

private static byte HexToByte(char val)
{
    if ((val <= '9') && (val >= '0'))
    {
        return (byte) (val - '0');
    }
    if ((val >= 'a') && (val <= 'f'))
    {
        return (byte) ((val - 'a') + 10);
    }
    if ((val >= 'A') && (val <= 'F'))
    {
        return (byte) ((val - 'A') + 10);
    }
    return 0xff;
}

【讨论】:

    【解决方案3】:

    在我看来,EnHex 和 DeHex 假设原始字符串中的字符是 ascii 编码的,或者在所有字符都在 0-255 范围内的其他字符集中进行编码。因此,所有字符都可以用两个字符的十六进制数字表示。以下 .NET (C#) 代码将解码您的十六进制编码字符串:

        public string DecodeHex(string input)
        {
            if (input.Length % 2 == 1)
                throw new ArgumentException("Invalid hex encoded string.");
    
            int len = input.Length / 2;
            StringBuilder output = new StringBuilder(len);
            for (int c = 0; c < len; ++c)
                output.Append((char)System.Convert.ToByte(input.Substring(c*2, 2), 16));
    
            return output.ToString();
        }
    

    这正是online hex decoder 正在做的事情。用它来测试你的结果和期望。

    【讨论】:

      【解决方案4】:

      只需使用Chr([hex code, e.g. &amp;H74])。唯一的问题是您需要自己解析代码,然后才能使用此解决方案。如果您有 unicode 字符,请改用 ChrW()

      http://msdn.microsoft.com/en-us/library/613dxh46(v=vs.71).aspx

      【讨论】:

        猜你喜欢
        • 2014-12-22
        • 2014-03-10
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        • 2018-01-31
        相关资源
        最近更新 更多