hex to unicode: problem

G

Guest

I use the following code to convert a hex string into unicode text. But the
problem is that it seems to stop after the first character and output
consists also of one character. Example...I want to convert "41004200" into
text. Normally it should be "AB" but it displays only the "A".


Dim output As String
Dim intIndex As Integer
Dim j As Byte
Dim ch As Char

For intIndex = 1 To Len(Hex.Text) Step 2
j = CByte("&H" & Mid(Hex.Text, intIndex, 2))
ch = Convert.ToChar(j)
output &= ch.ToString
Next
On Error Resume Next
Str.Text = output
 
T

Travers Naran

A good one! See below for answer
I use the following code to convert a hex string into unicode text. But the
problem is that it seems to stop after the first character and output
consists also of one character. Example...I want to convert "41004200" into
text. Normally it should be "AB" but it displays only the "A".


Dim output As String
Dim intIndex As Integer
Dim j As Byte
Dim ch As Char

For intIndex = 1 To Len(Hex.Text) Step 2
j = CByte("&H" & Mid(Hex.Text, intIndex, 2))

Maybe you meant?:
.... Step 4
.... Mid(Hex.Text, intIndex, 4)

What you've done in the original is:

Iter 1: Convert "41" and add to string
Iter 2: Convert "00" and add to string
Iter 3: Convert "42" and add to string
Iter 4: Convert "00" and add to string

The null character is probably interfering with displaying your string.
 
G

Guest

Thanks,

"The arithmetic operation had an overflow" comes when I try it.
I don't think it should be 4...


I use the same code to work with a hex string read out from a file.
Instead of the "output &= ch.ToString" there is FilePut(....) and its
written without any problems to a new file.
 
G

Guest

Maybe there is another way to convert hexstrings into unicode...
if anybody knows one, please tell me.

Thanks
 
B

Branco Medeiros

kenny said:
I use the following code to convert a hex string into unicode text. But the
problem is that it seems to stop after the first character and output
consists also of one character. Example...I want to convert "41004200" into
text. Normally it should be "AB" but it displays only the "A".
<snip>

One possible approach is to turn each two letters into a byte, store
the bytes in an array and ask one of the encoders from the Text
namespace to convert the array into a string:

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

HTH.

Regards,

B.
 
G

Guest

It works that way and there are no problems with 00 or many characters
Thank you Branco for helping me out!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top