将十六进制编码的字符串转换为 unicode 文本
我有像“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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有趣的问题。
谷歌搜索一下我在 VB.NET 中找到了这个
interesting problem.
googling a bit I found this in VB.NET
在我看来,EnHex 和 DeHex 假设原始字符串中的字符是 ascii 编码的,或者是用其他字符集编码的,其中所有字符都在 0-255 范围内。因此,所有字符都可以用两个字符的十六进制数字表示。以下 .NET (C#) 代码将解码您的十六进制编码字符串:
这正是这个在线十六进制解码器正在做。用它来测试您的结果和期望。
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:
This is exactly what this online hex decoder is doing. Use it to test your results and expectations.
只需使用
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 useChrW()
instead.http://msdn.microsoft.com/en-us/library/613dxh46(v=vs.71).aspx