ASP.NET_中文文字簡繁體並存網頁_中文文字轉unicode_unicode轉中文文字


由於在做HTTP傳輸(aspx 到 ashx 或另一aspx)時候
經常會被中文字亂碼而影響呈現
可能後端在vs驗證都是正常的可是到IIS跑就怪了....
因此在傳輸前都可轉為unicode形式之後到目的地再轉回來即可



程式碼


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'http://annion.kingbig.idv.tw/tool/column/web/unicode-ascii-ansi.htm
        'http://trufflepenne.blogspot.com/2013/03/cunicode.html
        Shared Function StrToUnicode(srcStr As String) As String
            Dim dstStr As String = ""
            Dim chArrSrc() As Char = srcStr.ToArray()
            For idx = 0 To UBound(chArrSrc)
                Dim byteArr() As Byte = Encoding.Unicode.GetBytes(chArrSrc(idx).ToString())
                Dim strTmp As String = "\u" & byteArr(1).ToString("X2") & byteArr(0).ToString("X2")
                dstStr &= strTmp
            Next
            Return dstStr
        End Function

        Shared Function UnicodeToStr(srcStr As String) As String
            Dim dstStr As String = ""
            Dim srcGroupLen As Integer = srcStr.Length / 6
            For idx = 0 To srcGroupLen - 1
                Dim strRes As String = ""
                strRes = srcStr.Substring(0, 6).Substring(2)
                srcStr = srcStr.Substring(6)
                Dim byteArr(2) As Byte
                byteArr(1) = Byte.Parse(Integer.Parse(strRes.Substring(0, 2), System.Globalization.NumberStyles.HexNumber).ToString())
                byteArr(0) = Byte.Parse(Integer.Parse(strRes.Substring(2, 2), System.Globalization.NumberStyles.HexNumber).ToString())
                dstStr &= Encoding.Unicode.GetString(byteArr)
                dstStr = dstStr.Substring(0, dstStr.Length - 1)
            Next
            Return dstStr
        End Function


Ref:
線上可驗證的工具
https://www.ifreesite.com/unicode-ascii-ansi.htm
https://www.chineseconverter.com/zh-tw/convert/unicode



留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header