将十六进制编码的字符串转换为 unicode 文本

发布于 2024-12-01 17:57:22 字数 750 浏览 0 评论 0原文

我有像“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

I have strings like "74657374696e67" (that is "testing"), hex encoded unicode text. Need to convert it back to readable output.
How to do this in .NET?

Update:

The text was originally encoded using the following Visual Basic 6 function:

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

The decoding was done as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在巴黎塔顶看东京樱花 2024-12-08 17:57:22

有趣的问题。

谷歌搜索一下我在 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

interesting problem.

googling a bit I found this in 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
挽清梦 2024-12-08 17:57:22
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;
}
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;
}
等风来 2024-12-08 17:57:22

在我看来,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();
    }

这正是这个在线十六进制解码器正在做。用它来测试您的结果和期望。

It seems to me that the EnHex and DeHex are assuming that the characters in the original string are ascii encoded, or encoded in some other character set where all the characters are in the range 0-255. Hence all characters can be represented by a two character hex number. The following .NET (C#) code will decode your hex encoded string:

    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();
    }

This is exactly what this online hex decoder is doing. Use it to test your results and expectations.

你在看孤独的风景 2024-12-08 17:57:22

只需使用 Chr([十六进制代码,例如 &H74])。唯一的问题是,您需要自己解析代码,然后才能使用此解决方案。如果您有 unicode 字符,请使用 ChrW() 代替。

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

Just use Chr([hex code, e.g. &H74]). The only thing is that you'll need to parse the code yourself before you can use this solution. If you have unicode characters use ChrW() instead.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文