Convert string to UTF8 string

Posted on 2008-10-29
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.
Question by:JiriNovotny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

ID: 22834089
Try this:

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

Author Comment

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

Expert Comment

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!
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

LVL 22

Accepted Solution

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.WriteText strUTF16
    objStream.Position = 0
    objStream.Type = adTypeBinary
    objStream.Read 3 ' skip BOM
    data = objStream.Read()
    ADO_EncodeUTF8 = StrConv(data, vbUnicode, 1033)
End Function

Open in new window


Assisted Solution

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

Circa 2000:

Background info via Microsoft Support KBs:
LVL 22

Expert Comment

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.    


Expert Comment

ID: 22842039
Here's a minor discussion about UTF conversion in VB6:

Heere's a tutorial in using Unicode (UTF) in VB6:

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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