How to replace unicode characters with their HTML codes.

  • Thread starter Thread starter Atara
  • Start date Start date
A

Atara

I have a file path that contains unicode characters.
e.g: "C:\vs2003\OutputDir???\mc_data\"
I want to translate this string to HTML with unicode characters:
e.g: "C:\vs2003\OutputDirאבג\data\"

I tried
strPath = System.Web.HttpUtility.HtmlEncode(strPath)
but it did not work.

I suppose I can create bytes from strPath and if I get (ascii > 127)
then replace "charVal" with "&charVal;"
but I am looking for a function that already does this algorithm...

any suggestions?
Thanks,
Atara
 
I found it in C# in http://conceptdev.blogspot.com/
Here is the vb code I use:

' --------------------------------------------------------
Private Function mcHtmlEntityEncode(ByVal unicodeText As String) As
String
' based on http://conceptdev.blogspot.com/
' ----------------------------------------------------
Dim unicodeVal As Integer
Dim encoded As String = ""
Dim c As Char

For Each c In unicodeText
unicodeVal = AscW(c) 'unicodeVal = c
Debug.WriteLine(c.ToString & " : " & unicodeVal.ToString)
If ((unicodeVal >= 49) And (unicodeVal <= 122)) Then
' in 'ascii' range x30 to x7a which is 0-9A-Za-z plus some
punctuation
encoded += c ' leave as-is
Else
' outside 'ascii' range - encode
encoded += String.Concat("&#",
unicodeVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)
, ";")
End If
Next
Return encoded
End Function
 
Back
Top