Convert hex-encoded string to unicode text
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
开发者_Python百科 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
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
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;
}
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.
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
精彩评论