将汉字转换为Unicode编码vb源码
vb中,将汉字转换为Unicode编码过程,参数source为需要转换⽂字字符串,Hex为Boolean类型,表⽰需要转为⼗六进制的,还是⼗
进制的编码字符串。
''' <summary>
''' 将Unicode字符串转为汉字
''' </summary>
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(C End Function
''' <summary>
''' 将汉字转换为Unicode字符串
''' </summary>
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论