EXCEL计算汉字的GBK码和UNICOD的自定义函数

<span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;">方法:EXCEL->菜单"工具"->“宏”->“Visual Basic编辑器";</span>
<span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;"><span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 微软雅黑, arial, 宋体, sans-serif; font-size: 16px; line-height: 28px; text-align: justify;">"Visual Basic编辑器-Book1”窗口->“插入”->“模块</span>
</span>

Function GetUnicode(str As String) As String  
'输出str的unicode码  
    Dim chrTmp As String  
    Dim ByteLower As String  
    Dim ByteUpper As String  
    Dim strReturn As String  
    For i = 1 To Len(str)  
        chrTmp$ = Mid(str, i, 1)  
        ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))  
        If Len(ByteLower$) = 1 Then '如果低字节长度为1,补一个0  
            ByteLower$ = "0" & ByteLower$  
        End If  
        ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))  
        If Len(ByteUpper$) = 1 Then '如果高字节长度为1,补一个0  
            ByteUpper$ = "0" & ByteUpper$  
        End If  
        strReturn$ = "0x" & ByteUpper$ & ByteLower$  
    Next  
    GetUnicode = strReturn
End Function

Function GetUnicodeDec(str As String) As String  
'输出str的unicode码值 
    Dim chrTmp As String  
    Dim ByteLower As String  
    Dim ByteUpper As String  
    chrTmp$ = Mid(str, i, 1)  
    ByteLower$ = AscB(MidB$(chrTmp$, 1, 1))
    ByteUpper$ = AscB(MidB$(chrTmp$, 2, 1)) 
    GetUnicodeDec = ByteUpper*256 + ByteLower
End Function


Function GetGBK(strInput As String) As String
'输出str的gbk码
    Dim x() As Byte
    Dim i As Integer
    x = strInput
    x = StrConv(x, vbFromUnicode)
    If (UBound(x) - LBound(x) + 1) / Len(strInput) <> 2 Then
        GetGBK = "你输入包含非中文字符"
    Else
        For i = 0 To UBound(x) Step 2
            GetGBK = GetGBK & Hex(x(i<span style="font-family: Arial, Helvetica, sans-serif;">+ 1</span><span style="font-family: Arial, Helvetica, sans-serif;">)) & Hex(x(i))</span>
        Next i
        GetGBK = Trim(GetGBK)
    End If
End Function


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值