Link to home
Start Free TrialLog in
Avatar of DistributedServices
DistributedServices

asked on

handling Unicode in VB6: How to replace / convert to ansi or to find in a text string?

Hi there,

I need to replace a unicode character in a string (like StringVar=Replace (StringVar,"►","_") ) ?

I read a string from an <input > in a webbrowser object. I need to use the string as a file name but I can't if unicode characters entered. Usually VB converts to "?" and displays it. Of cause i tried  StringVar=Replace (StringVar,"?","_") but that does not help. It seams StringVar contains no "?". It seams VB converts unicode to ansi only for displaying not for something else.

Any idea or tip how to replace unicode with ansi in a string variable?

Thank you very much.

Christine
ASKER CERTIFIED SOLUTION
Avatar of Om Prakash
Om Prakash
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

You can try
Debug.Print StrConv("Test123", vbFromUnicode)
Debug.Print StrConv("Test123", vbUnicode)
Hi!
;&#9658 these are unicode characters as the browser represents them as source code.
You can save them directly to database and for example in APS app you will get them in same form from input box where user do search functions. They work.

Each language has a certain range of them, there might be no ansi characters similar but close.
You need a select case structure to convert these unicode numbers to ansi which you have compared to be very much similar, like a controlled transliteration.

In VB.NET you will have more options to do this, but it looks like you are going to do this in browser then VB 6.0 can do it but certain tricks

Regards!
Matti

Regards!
Matti
Avatar of Dana Seaman
This function "RemoveUni" will replace any Unicode characters with "?".
It makes use of a byte array so it is quite fast.

Option Explicit
 
Private Sub Form_Load()
   Dim sUni    As String
   Dim sAnsii  As String
   
   sUni = "CHS: " & ChrW$(&H6B22) & ChrW$(&H8FCE)
   sAnsii = RemoveUni(sUni, 63) 'Replace with "?"
   Debug.Print sAnsii
    
End Sub
 
Public Function RemoveUni(ByVal s As String, ByVal ReplaceWith As Byte) As String
   Dim i                As Long
   Dim bLen             As Long
   Dim Map()            As Byte
 
   If LenB(s) Then
      Map = s
      bLen = UBound(Map)
      For i = 1 To bLen Step 2
         If (Map(i) > 0) Then 'Is Unicode
            Map(i) = 0 'Clear upper byte
            Map(i - 1) = ReplaceWith 'Replace low byte
         End If
      Next
   End If
   RemoveUni = Map
End Function

Open in new window

Avatar of DistributedServices

ASKER

That helped most:
   For i = 1 To Len(strSrc)
        sChar = Mid(strSrc, i, 1)
        If (AscW(mid(strSrc,i,1)) > 255) Then
            Mid(strSrc,i,1) = "?"
        End If
    Next i

I made it:

Function Unicode2HtmlNotation(ByVal l_UnicodeString)
    Dim m_Cnt, m_Char
    Unicode2HtmlNotation = l_UnicodeString
    For m_Cnt = 1 To Len(l_UnicodeString)
        m_Char = Mid(l_UnicodeString, m_Cnt, 1)
        If (AscW(Mid(l_UnicodeString, m_Cnt, 1)) > 255) Then
            If InStr("{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±°¹²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùú", m_Char) = 0 Then
                Debug.Print AscW(Mid(l_UnicodeString, m_Cnt, 1))
                Debug.Print Unicode2HtmlNotation
                Unicode2HtmlNotation = Left(Unicode2HtmlNotation, m_Cnt - 1) & "&#" & Trim(Str(AscW(Mid(Unicode2HtmlNotation, m_Cnt, 1)))) & ";" & Mid(Unicode2HtmlNotation, m_Cnt + 1)
                Debug.Print Unicode2HtmlNotation
            End If
        End If
    Next m_Cnt
End Function

Function Unicode2HtmlNotation(ByVal l_UnicodeString)
    Dim m_Cnt, m_Char
    Unicode2HtmlNotation = l_UnicodeString
    For m_Cnt = 1 To Len(l_UnicodeString)
        m_Char = Mid(l_UnicodeString, m_Cnt, 1)
        If (AscW(Mid(l_UnicodeString, m_Cnt, 1)) > 255) Then
            If InStr("{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±°¹²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùú", m_Char) = 0 Then
                Debug.Print AscW(Mid(l_UnicodeString, m_Cnt, 1))
                Debug.Print Unicode2HtmlNotation
                Unicode2HtmlNotation = Left(Unicode2HtmlNotation, m_Cnt - 1) & "&#" & Trim(Str(AscW(Mid(Unicode2HtmlNotation, m_Cnt, 1)))) & ";" & Mid(Unicode2HtmlNotation, m_Cnt + 1)
                Debug.Print Unicode2HtmlNotation
                'Mid(l_UnicodeString, m_Cnt, 1) = "&#" & Trim(Str(AscW(Mid(l_UnicodeString, m_Cnt, 1)))) & ";" '&#9658;
            End If
        End If
    Next m_Cnt
End Function

Open in new window