Home All Groups Group Topic Archive Search About

hex to unicode: problem

Author
25 Aug 2006 2:10 PM
kenny
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

Author
25 Aug 2006 2:30 PM
Travers Naran
A good one!  See below for answer

kenny wrote:
> 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.
Author
25 Aug 2006 3:31 PM
kenny
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.

"Travers Naran" schrieb:

Show quoteHide quote
> A good one!  See below for answer
>
> kenny wrote:
> > 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.
>
>
Author
26 Aug 2006 11:53 AM
kenny
Maybe there is another way to convert hexstrings into unicode...
if anybody knows one, please tell me.

Thanks

"kenny" schrieb:

Show quoteHide quote
> 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.
>
> "Travers Naran" schrieb:
>
> > A good one!  See below for answer
> >
> > kenny wrote:
> > > 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.
> >
> >
Author
26 Aug 2006 10:17 PM
Branco Medeiros
kenny wrote:
> 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.
Author
27 Aug 2006 12:39 PM
kenny
It works that way and there are no problems with 00 or many characters
Thank you Branco for helping me out!

Show quoteHide quote
"Branco Medeiros" wrote:

>
> kenny wrote:
> > 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.
>
>