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
DistributedServicesAsked:
Who is Participating?
 
Om PrakashCommented:
0
 
nffvrxqgrcfqvvcCommented:
You can try
Debug.Print StrConv("Test123", vbFromUnicode)
Debug.Print StrConv("Test123", vbUnicode)
0
 
MattiCommented:
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
danaseamanCommented:
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

0
 
DistributedServicesAuthor Commented:
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

0
 
DistributedServicesAuthor Commented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.