Question Title: How can UTF8 convert to ANSI(BIG5) and ANSI(BIG5) convert to UTF8

Dear,

I have two database(MYSQL, MSSQL). MYSQL is UTF8, MSSQL is ANSI(BIG5), I need to write a VB programming to transfer data btw them.
But I have no any idea to these function. Would you mind help me to solve the problem??
KahgusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RanjeetRainCommented:
You can do with Chilkat Charset ActiveX. See this: http://www.chilkatsoft.com/Charset-ActiveX.asp
0
KahgusAuthor Commented:
I want to know how to use VB code to do these function???
0
Bob LambersonSoftware EngineerCommented:
Check out MS knowledgebase Q198862 and Q142607.
Also ARTICLE-ID: Q136477
TITLE : How to Install MingLiU Fonts for BIG5 Applications

ARTICLE-ID: Q166426
TITLE : Internet Explorer in PCNTW Cannot view Chinese BIG5 Characters

They are also in the MSDN oct2001 help files for vb6.



Bob
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

KahgusAuthor Commented:
I just want convert utf8 to ansi for using vb code..
Please help me
0
Bob LambersonSoftware EngineerCommented:
Did you read the articles, and were they of any help?

Bob
0
KahgusAuthor Commented:
I have readed the articles, but i have no any idea.
Anyone have VB sample code for "UTF-8 to ANSI"???
THX
0
Bob LambersonSoftware EngineerCommented:
http://www.motobit.com/help/scptutl/sa298.htm

I was able to find this on the web....

 This will convert any text into utf8
Coded by Wolf of Boris, Sweeden
Code:
Function Encode_UTF8(astr$)

  Dim c As Long
  Dim utftext As String
    utftext = ""
    For n = 1 To Len(astr$)
        c = AscW(Mid(astr$, n, 1))
        If c < 128 Then
            utftext = utftext + Mid(astr$, n, 1)
        ElseIf ((c > 127) And (c < 2048)) Then
            utftext = utftext + Chr(((c \ 64) Or 192))
            utftext = utftext + Chr(((c And 63) Or 128))
        Else
            utftext = utftext + Chr(((c \ 144) Or 234))
            utftext = utftext + Chr((((c \ 64) And 63) Or 128))
            utftext = utftext + Chr(((c And 63) Or 128))
        End If
    Next n

  Encode_UTF8 = utftext

End Function

Also see this page http://www.icarz.com/mysql/

Bob
0
KahgusAuthor Commented:
Dear Bob and anyone,

What about UTF-8 to ANSI???
If use WideCharToMultiByte or MultiByteToWideCharAPI, how to write vb code for two function???

Thank!

Kahgus
0
jimbobmcgeeCommented:
>> I just want convert utf8 to ansi for using vb code...

Using MultiByteToWideChar (if set up correctly) will convert UTF8 to Unicode.  VB6 uses Unicode in all its strings (it just has a problem displaying them in the standard controls).  To display Unicode strings in controls, you need the Microsoft Forms 2.0 Object Library.  This contains a number of controls, similar to those in VB6, that are Unicode compliant.  

You can get the Forms 2.0 library (fm20.dll) by installing Office or the ActiveX Control Pad (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaxctrl/html/cpad.asp).

HTH

J.
0
Bob LambersonSoftware EngineerCommented:
From the help files:
"When you store the characters to a String variable or get the characters from a String variable, Visual Basic automatically converts between Unicode and ANSI characters." Since vb uses Unicode, anything you get from a variable dimmed as a string wil be Unicode.
If you get a string value from your mysql and store it in a string variable then when you use the string from that variable it will be Unicode.

If you then want to convert the string to utf8 use the code above that converts any string to utf8 encoding.
Which is

 This will convert any text into utf8
Coded by Wolf of Boris, Sweeden
Code:
Function Encode_UTF8(astr$)

  Dim c As Long
  Dim utftext As String
    utftext = ""
    For n = 1 To Len(astr$)
        c = AscW(Mid(astr$, n, 1))
        If c < 128 Then
            utftext = utftext + Mid(astr$, n, 1)
        ElseIf ((c > 127) And (c < 2048)) Then
            utftext = utftext + Chr(((c \ 64) Or 192))
            utftext = utftext + Chr(((c And 63) Or 128))
        Else
            utftext = utftext + Chr(((c \ 144) Or 234))
            utftext = utftext + Chr((((c \ 64) And 63) Or 128))
            utftext = utftext + Chr(((c And 63) Or 128))
        End If
    Next n

  Encode_UTF8 = utftext

End Function

I have not tested this so you are on your own there.

Bob
0
KahgusAuthor Commented:
Everybody,

I found some code in WEB for ANSI TO UTF-8:

Public Function UTF8_Encode(ByVal Text As String) As String

Dim sBuffer As String
Dim lLength As Long

If Text <> "" Then
lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Text), -1, 0, 0, 0, 0)
sBuffer = Space$(lLength)
lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Text), -1, StrPtr(sBuffer), Len(sBuffer), 0, 0)
sBuffer = StrConv(sBuffer, vbUnicode)
UTF8_Encode = Left$(sBuffer, lLength - 1)
Else
UTF8_Encode = ""
End If

End Function

But I can't found VB code about convert UTF-8 TO ANSI(BIG5).
How can I use this method to convert UTF-8 TO ANSI(BIG5)??
Please help me!!!
0
jimbobmcgeeCommented:
As per Microsoft (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_81rn.asp), the code page for Big5 is 10002 (MAC - Traditional Chinese (Big5)).

I imagine you would invert the above function (using MultiByteToWideChar), to decode from UTF8 to Unicode, and then re-encode (with WideCharToMultiByte) with the value 10002 instead of CP_UTF8.

I can't test it, as I have no data to play with, but the following makes the most sense to me (paste into a module):

    Private Declare Function MultiByteToWideChar _
                    Lib "kernel32" (ByVal CodePage As Long, _
                                    ByVal dwFlags As Long, _
                                    ByVal lpMultiByteStr As String, _
                                    ByVal cchMultiByte As Long, _
                                    ByVal lpWideCharStr As String, _
                                    ByVal cchWideChar As Long) _
                    As Long
                                                                 
    Private Declare Function WideCharToMultiByte _
                    Lib "kernel32" (ByVal CodePage As Long, _
                                    ByVal dwFlags As Long, _
                                    ByVal lpWideCharStr As String, _
                                    ByVal cchWideChar As Long, _
                                    ByVal lpMultiByteStr As String, _
                                    ByVal cchMultiByte As Long, _
                                    ByVal lpDefaultChar As String, _
                                    ByVal lpUsedDefaultChar As Long) _
                    As Long
                                                                 
   
    Private Const CP_UTF8 = 65001       'CODE PAGE FOR UTF8
    Private Const CP_BIG5 = 10002       'CODE PAGE FOR BIG5
   
   
    Public Function ConvertUTF8ToBIG5(ByVal sText As String)
   
        Dim sBuffer  As String
        Dim nLength  As Long
        Dim byText() As Byte
       
        '''' CONVERT UTF8 TO UNICODE '''''''''''''''''''''''''''''''''''''''''''''''
        If LenB(sText) = 0 Then
            ConvertUTF8ToBIG5 = ""
            Exit Function
        End If                          'IF NOTHING TO CONVERT, DON'T BOTHER
       
        byText() = StrConv(sText, vbFromUnicode)
        nLength = UBound(byText()) + 1
        sBuffer = String(nLength * 2, vbNullChar)
       
        nLength = MultiByteToWideChar(CP_UTF8, 0, byText(0), nLength, _
                                      StrPtr(sBuffer), nLength * 2)
                                        'CONVERT TO UNICODE AND STORE IN sBuffer
                                     
        If nLength > 0 Then sText = Left(sBuffer, nLength)
                                        'TRIM IF REQUIRED
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       
        '''' CONVERT UNICODE TO BIG5 '''''''''''''''''''''''''''''''''''''''''''''''
        If LenB(sText) = 0 Then
            ConvertUTF8ToBIG5 = ""
            Exit Function
        End If                          'IF NOTHING TO CONVERT, DON'T BOTHER
       
        ReDim byText(LenB(sText) * 8)   'MAKE A BIG BUFFER TO STORE TEXT
       
        nLength = WideCharToMultiByte(CP_BIG5, 0, StrPtr(sText), Len(sText), _
                                      byText(0), LenB(sText) * 8, vbNullString, 0)
                                        'CONVERT TO BIG5 AND STORE IN byText
                                     
        If nLength > 0 Then
            ReDim Preserve byText(nLength - 1)
                                        'RESIZE byText TO FIT ACTUAL CONTENTS
            ConvertUTF8ToBIG5 = StrConv(byText, vbUnicode)
                                        'RETURN BIG5 BYTE PATTERN AS STRING
        Else
            ConvertUTF8ToBIG5 = ""      'IF NO LENGTH, RETURN NOTHING
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       
    End Function

http://www.cyberactivex.com/UnicodeTutorialVb.htm offers some great tutorials on Unicode and character set conversion...

HTH

J.

0
jimbobmcgeeCommented:
BTW, you call the code above with:

    myBIG5String = ConvertUTF8ToBIG5(myUTF8String)

Rgds,

J.
0
jimbobmcgeeCommented:
...and converting from BIG5 to UTF8 should just be a case of copying the function a second function and changing:

      nLength = MultiByteToWideChar(CP_UTF8, 0, byText(0), nLength, StrPtr(sBuffer), nLength * 2)

to

      nLength = MultiByteToWideChar(CP_BIG5, 0, byText(0), nLength, StrPtr(sBuffer), nLength * 2)

and

      nLength = WideCharToMultiByte(CP_BIG5, 0, StrPtr(sText), Len(sText), byText(0), LenB(sText) * 8, vbNullString, 0)

to

      nLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), byText(0), LenB(sText) * 8, vbNullString, 0)

Rgds,

J.
0
KahgusAuthor Commented:
jimbobmcgee,

Thank you for your hand.

I had try your vb code, but this code have error, VB is auto to close.

Please help me again.
0
jimbobmcgeeCommented:
What error are you receiving and on what line?  Or is VB just crashing to desktop?  The code was provided untested (as I have no BIG5 data to test with).

J.
0
KahgusAuthor Commented:
Jimbobmcgee,

is Visual Basic just to close by error, The error is bout kernel32.dll.
Maybe you try your code for other CodePsge Data, the error is also display in screen.

Please help me!
Thank!

Kahgus
0
RanjeetRainCommented:
Hi Kahgus,

Please check out this section. This is where your answer lies: http://www.chilkatsoft.com/refdoc/xChilkatCharsetRef.html#method002

YOu have to set two properties and call the ConvertData method, which converts character data from one charset to another. Before calling ConvertData, you must set the FromCharset and ToCharset properties to the source and destination charset names.
0
jimbobmcgeeCommented:
Unfortunately, I can't replicate the error.  Try changing the CP_BIG5 variable to 950.

However, I also cannot get the MultiByteToWideChar API call to actually return a value.  If anyone can correctly implement the MultiByteToWideChar call, the code I posted _might_ work...

J.
0
PAQ_ManCommented:
PAQed with no points refunded (of 500)

PAQ_Man
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.