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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Om PrakashCommented:
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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
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.