473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

hex to unicode: problem

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
Aug 25 '06 #1
5 2750
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.

Aug 25 '06 #2
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.

Aug 25 '06 #3
Maybe there is another way to convert hexstrings into unicode...
if anybody knows one, please tell me.

Thanks

"kenny" schrieb:
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.
Aug 26 '06 #4

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.

Aug 26 '06 #5
It works that way and there are no problems with 00 or many characters
Thank you Branco for helping me out!

"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.

Aug 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sebastien.hugues | last post by:
Hi I would like to retrieve the application data directory path of the logged user on windows XP. To achieve this goal i use the environment variable APPDATA. The logged user has this name:...
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
14
by: wolfgang haefelinger | last post by:
Hi, I wonder whether someone could explain me a bit what's going on here: import sys # I'm running Mandrake 1o and Windows XP. print sys.version ## 2.3.3 (#2, Feb 17 2004, 11:45:40)
19
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
6
by: peter pilsl | last post by:
postgres 7.3.2 I store unicode-data in postgresql. The data is retrieved via webinterfaces, processed with perl and then stored in postgresql (and viceversa). All is going nice with one...
1
by: Paul | last post by:
Hi, I am extending an existing MFC app to use Unicode (for a Japanese version of the interface elements). The app's purpose is to control a peripheral device through the serial port, and the...
3
by: Kidus Yared | last post by:
I am having a problem displaying Unicode characters on my Forms labels and buttons. After coding Button1.Text = unicode; where the unicode is a Unicode character or string (‘\u1234’ or...
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
5
by: Holger Joukl | last post by:
Hi there, I consider the behaviour of unicode() inconvenient wrt to conversion of non-string arguments. While you can do: u'17.3' you cannot do:
5
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.