将汉字转换为Unicode编码vb.net源码

vb.net中,将汉字转换为Unicode编码过程,参数source为需要转换文字字符串,Hex为Boolean类型,表示需要转为十六进制的,还是十进制的编码字符串。

  ''' 
  ''' 将Unicode字符串转为汉字
  ''' 
  Function UnicodeToStr(ByVal source As String) As String
    Return New Regex("&#x([0-9A-F]{4});", RegexOptions.IgnoreCase Or RegexOptions.Compiled).Replace(source, Function(x) String.Empty & Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)))
  End Function

  ''' 
  ''' 将汉字转换为Unicode字符串
  ''' 
  Function StrToUnicode(ByVal source As String, Optional Hex As Boolean = True) As String
    Dim bytes As Byte() = Encoding.Unicode.GetBytes(source), Str As String = ""
    For i As Integer = 0 To bytes.Count - 1 Step 2
      If Hex Then Str &= "&#x" & bytes(i + 1).ToString("x") + bytes(i).ToString("x") & ";" Else Str &= "&#" & bytes(i + 1) * 256 + bytes(i) & ";"
    Next
    Return Str
  End Function

 

你可能感兴趣的:(将汉字转换为Unicode编码vb.net源码)