By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

ANSI <--> Unicode conversion VB6 compliant with any codepage

P: n/a
Hello everybody,

since days i try to convert Unicode-Strings in VB.NET to ANSI which should
be processable in VB6 and converted to unicode again.
It should be possible with any codepage, e.g. somebody on a greek PC should
be able to handle chinese strings

-------------- VB.NET ----------------
'On the vb.net side (I am pretty sure, it is correct(?))
Dim oEncoderAnsi As System.Text.Encoding
Dim oEncoderSource As System.Text.Encoding

oEncoderSource = System.Text.Encoding.Unicode
oEncoderAnsi = System.Text.Encoding.GetEncoding(950) 'Chinese codepage

bufWriteTemp = oEncoderSource.GetBytes(toWrite)
bufWrite = System.Text.Encoding.Convert(oEncoderSource, oEncoderAnsi,
bufWriteTemp)

'The output of the re-converted byteArray looks good (chinese characters
again).
Debug.WriteLine("back: " & oEncoderAnsi.GetString(bufWrite))

-----------------------------------------

-------------- VB6 ---------------------
'On the VB6 side the ANSI-String comes in
'I tried the functions of Michael Kaplan:
temp = WToA(temp.Value, 950)
temp = AToW(temp, 950)

'These work fine inside VB6 viceversa. But not with the string coming from
the application above.
'e.g. 3 chinese characters are 6 in ANSI with WtoA (OK). But the ones
converted by .NET abover are 10 characters long now.
'I also tried
strconv(temp, vbUnicode, 2052)

'With strconv I am not sure if the LCID really matches the chosen codepage.
-----------------------------------------

How can i debug the whole thing to see on which side the problem lies?
Does somebody know good resources about this (i have the Book from MichKa,
which only covers VB6)?

Thank you very much!
Dirk


Jan 25 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"msdnuniv" <nb@nowhere123.comschrieb:
since days i try to convert Unicode-Strings in VB.NET to ANSI which should
be processable in VB6 and converted to unicode again.
It should be possible with any codepage, e.g. somebody on a greek PC
should be able to handle chinese strings
I suggest to describe the exact scenario in more detail, because VB6 is
basically Unicode-enabled (except file access statements and forms).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 25 '07 #2

P: n/a
"msdnuniv" <nb@nowhere123.comschrieb
Hello everybody,

since days i try to convert Unicode-Strings in VB.NET to ANSI which
should be processable in VB6 and converted to unicode again.
It should be possible with any codepage, e.g. somebody on a greek PC
should be able to handle chinese strings

-------------- VB.NET ----------------
'On the vb.net side (I am pretty sure, it is correct(?))
Dim oEncoderAnsi As System.Text.Encoding
Dim oEncoderSource As System.Text.Encoding

oEncoderSource = System.Text.Encoding.Unicode
oEncoderAnsi = System.Text.Encoding.GetEncoding(950) 'Chinese
codepage

bufWriteTemp = oEncoderSource.GetBytes(toWrite)
bufWrite = System.Text.Encoding.Convert(oEncoderSource,
oEncoderAnsi, bufWriteTemp)

'The output of the re-converted byteArray looks good (chinese
characters again).
Debug.WriteLine("back: " & oEncoderAnsi.GetString(bufWrite))

-----------------------------------------
I am not sure what's the purpose of the code above. If you need the CP 950
encoded byte array, simply call

ByteBuffer = oEncoderAnsi.getbytes(string)

-------------- VB6 ---------------------
'On the VB6 side the ANSI-String comes in

How does it come in? Did you create a COM DLL in VB.Net?
'I tried the functions of Michael Kaplan:
temp = WToA(temp.Value, 950)
temp = AToW(temp, 950)

'These work fine inside VB6 viceversa. But not with the string
coming from the application above.
Do you expect an CP 950 encoded array?
'e.g. 3 chinese characters are 6 in ANSI with WtoA (OK). But the
What ist WtoA/AToW? What is temp? How is ist declared?
ones converted by .NET abover are 10 characters long now.
'I also tried
strconv(temp, vbUnicode, 2052)

'With strconv I am not sure if the LCID really matches the chosen
codepage.
-----------------------------------------
>
How can i debug the whole thing to see on which side the problem
lies? Does somebody know good resources about this (i have the Book
from MichKa, which only covers VB6)?
Is the main Exe in VB6 or VB.Net?
In general, there are no "Chinese strings". You can only have a byte array
containing a CP 950 encoded String. Strings (System.String) in .Net are
internally always stored as Unicode.
Armin

Jan 25 '07 #3

P: n/a
Hi Herfried and Armin,

thank you!
I try to explain a bit more:
>-------------- VB.NET ----------------
'On the vb.net side (I am pretty sure, it is correct(?))
Dim oEncoderAnsi As System.Text.Encoding
Dim oEncoderSource As System.Text.Encoding

oEncoderSource = System.Text.Encoding.Unicode
oEncoderAnsi = System.Text.Encoding.GetEncoding(950) 'Chinese
codepage

bufWriteTemp = oEncoderSource.GetBytes(toWrite)
bufWrite = System.Text.Encoding.Convert(oEncoderSource,
oEncoderAnsi, bufWriteTemp)

'The output of the re-converted byteArray looks good (chinese
characters again).
Debug.WriteLine("back: " & oEncoderAnsi.GetString(bufWrite))

-----------------------------------------

I am not sure what's the purpose of the code above. If you need the CP 950
encoded byte array, simply call

ByteBuffer = oEncoderAnsi.getbytes(string)
This is exactly what i'doing.
The purpose is to get an ansi (CP 950 or any other) encoded byte array to
send it to a mailslot, which is maintained by a VB6-app.
How does it come in? Did you create a COM DLL in VB.Net?
No, as a message in a mailslot. (I tried also to do base64 encoding on the
unicode strings but did not find an algorithm which can decode the
base64-string i made with vb.net

>
>'I tried the functions of Michael Kaplan:
temp = WToA(temp.Value, 950)
temp = AToW(temp, 950)

'These work fine inside VB6 viceversa. But not with the string
coming from the application above.
Do you expect an CP 950 encoded array?
Yes, above i encoded unicode to CP 950 and on the VBA side i expect it to be
950 in this case.

>'e.g. 3 chinese characters are 6 in ANSI with WtoA (OK). But the

What ist WtoA/AToW? What is temp? How is ist declared?
temp is just a string.
WtoA, AtoW are just handy functions to convert to/from unicode from famous
Michael Kaplan (www.trigeminal.com)

>ones converted by .NET abover are 10 characters long now.
'I also tried
strconv(temp, vbUnicode, 2052)

'With strconv I am not sure if the LCID really matches the chosen
codepage.
-----------------------------------------
>>
How can i debug the whole thing to see on which side the problem
lies? Does somebody know good resources about this (i have the Book
from MichKa, which only covers VB6)?

Is the main Exe in VB6 or VB.Net?
There is no "main". There are two independend applications (one .NET, one
VBA, talking to each other over mailslots.
If i use mailslotW to send unicode directly, Access 2003 crashes
immediately. I could not decode base64-encoded unicode-Strings on the
VB-Side.
So i try to convert to ANSI first and send that.
The problem is, that it must work with any codepage, not only default.

In general, there are no "Chinese strings". You can only have a byte array
containing a CP 950 encoded String. Strings (System.String) in .Net are
internally always stored as Unicode.
Yes, I know, that's why i create a bytearray, which is sent by API-function
writefile into the mailslot.
>
Armin
Greetings,
Dirk
Jan 29 '07 #4

P: n/a
"msdnuniv" <nb@nowhere123.comschrieb

ByteBuffer = oEncoderAnsi.getbytes(string)

This is exactly what i'doing.
The purpose is to get an ansi (CP 950 or any other) encoded byte
array to send it to a mailslot, which is maintained by a VB6-app.
I read your message but I think I can't help you: I do not yet know how
mailslots work. I also don't know the stuff from www.trigeminal.com. Also, I
didn't find "mailslotw" anywhere.

If you use Writefile to write into the mailslot, why not use
sytem.text.encoding.unicode to get the byte array? How do you read the data
in the destination application? If I got it right, it is Acc2003 (VBA) which
should be able to use Strconv to get the string from the unicode encoded
array.

Armin

Jan 29 '07 #5

P: n/a
Armin,
thank you!
strconv I already tried (see initial posting).
If i write a unicode-encoded ByteArray into the mailslot, I get garbage in
condition (also with sub-128 characters).
Trigeminal.com ist the site of Michael Kaplan, who wrote the bible
"Internationalization with Visual Basic".

Greetings, Dirk
"Armin Zingler" <az*******@freenet.deschrieb im Newsbeitrag
news:eo**************@TK2MSFTNGP05.phx.gbl...
"msdnuniv" <nb@nowhere123.comschrieb
>
ByteBuffer = oEncoderAnsi.getbytes(string)

This is exactly what i'doing.
The purpose is to get an ansi (CP 950 or any other) encoded byte
array to send it to a mailslot, which is maintained by a VB6-app.

I read your message but I think I can't help you: I do not yet know how
mailslots work. I also don't know the stuff from www.trigeminal.com. Also,
I didn't find "mailslotw" anywhere.

If you use Writefile to write into the mailslot, why not use
sytem.text.encoding.unicode to get the byte array? How do you read the
data in the destination application? If I got it right, it is Acc2003
(VBA) which should be able to use Strconv to get the string from the
unicode encoded array.

Armin

Jan 29 '07 #6

P: n/a
"msdnuniv" <nb@nowhere123.comschrieb
Armin,
thank you!
strconv I already tried (see initial posting).
If i write a unicode-encoded ByteArray into the mailslot, I get
garbage in condition (also with sub-128 characters).
Why garbage? How do you see that it is garbage? Can you give an example of
what you send and what you receive?
Armin

Jan 29 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.