|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
hex to unicode: problemI 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 A good one! See below for answer
kenny wrote: > I use the following code to convert a hex string into unicode text. But the Maybe you meant?:> 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)) .... 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. 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. > > 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. > > > > kenny wrote:
> I use the following code to convert a hex string into unicode text. But the <snip>> 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". > 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. 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. > >
Saving changes made in DataGridView
Atomic Operation? One Last Class Question - Example that works DirectX rendered inside a control? problem save and reading from the registry Database Problems with SaveAs Current namespace, stack and lineno How to make double click event to call and execute the mouseup event code I/O Access through window handle? |
|||||||||||||||||||||||