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

vb.net中,将汉字转换为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(Convert.ToUInt16(x.Result("$1"), 16)))
  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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值