Solved

Convert string to UTF8 string

Posted on 2008-10-29
7
3,693 Views
Last Modified: 2010-02-03
I want a visual basic 6.0 procedure that will convert a string to a string containing UTF8 encoded text.
0
Comment
Question by:JiriNovotny
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:nasserd
ID: 22834089
Try this:
http://www.nonhostile.com/howto-convert-byte-array-utf8-string-vb6.asp

Remember that UTF-8 is treated as a Byte array; this should apply throughout your application and database layers.
0
 

Author Comment

by:JiriNovotny
ID: 22834485
Hi nasserd, thanks for your comment. The solution you provided is for VB.NET, I need VB6 solution.
0
 
LVL 4

Expert Comment

by:nasserd
ID: 22835525
Hi, the solution I directed you to is actually VB6.  There is even a Zip file you can download which has a project in VB6.

UTF-8 conversion in .NET is *very* different!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Accepted Solution

by:
danaseaman earned 250 total points
ID: 22837558
Vb6 strings are stored as UTF-16
Here is Function to convert Vb6 string to UTF-8 string.:


Option Explicit
 

Private Const adTypeBinary    As Long = 1

Private Const adTypeText      As Long = 2

Private Const adModeReadWrite As Long = 3
 
 

'Purpose: UTF16 to UTF8 using ADO

Public Function ADO_EncodeUTF8(ByVal strUTF16 As String) As String
 

    Dim objStream As Object

    Dim data() As Byte

    

    Set objStream = CreateObject("ADODB.Stream")

    objStream.Charset = "utf-8"

    objStream.Mode = adModeReadWrite

    objStream.Type = adTypeText

    objStream.Open

    objStream.WriteText strUTF16

    objStream.Flush

    objStream.Position = 0

    objStream.Type = adTypeBinary

    objStream.Read 3 ' skip BOM

    data = objStream.Read()

    objStream.Close

    ADO_EncodeUTF8 = StrConv(data, vbUnicode, 1033)
 

End Function

Open in new window

0
 
LVL 4

Assisted Solution

by:nasserd
nasserd earned 250 total points
ID: 22837772
Persuant to danaseaman's StrConv() recommendation, here are two relevant posts:

Circa 2000: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_10662022.html?eeSearch=true

Background info via Microsoft Support KBs: http://support.microsoft.com/kb/q179845/
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 22839661
Second link deals with obsolete 16-bit Vb4 vs 32-bit Vb5/6 and he is using Vb6 so this is not an issue.  

First link deals with UTF8 StrConv issues when running on a Japanese OS. If you use StrConv without specifying a LocalID on a Japanese system (which is MBCS) your UTF-8 conversion can be incorrrect since StrConv uses the current language. This is why above code uses 1033 to force StrConv to use U.S. English which does not have MBCS issues.    

0
 
LVL 4

Expert Comment

by:nasserd
ID: 22842039
Here's a minor discussion about UTF conversion in VB6:
http://www.xtremevbtalk.com/archive/index.php/t-281422.html

Heere's a tutorial in using Unicode (UTF) in VB6:
http://www.cyberactivex.com/unicodetutorialvb.htm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now