字节数组和字符串按十六进制互相转换

Posted on 2010-08-10 10:51  严武  阅读(179)  评论(0编辑  收藏  举报

Private   Function   Byte2HexStr(bt()   As   Byte)   As   String
Dim   btLen       As   Integer
Dim   I               As   Integer
Dim   strDes     As   String
        btLen   =   UBound(bt)   +   1
        strDes   =   Space$(btLen   *   2)
        For   I   =   0   To   btLen   -   1
                Mid$(strDes,   I   +   I   +   1,   2)   =   Right$( "0 "   &   Hex$(bt(I)),   2)
        Next   I
        Byte2HexStr   =   strDes
End   Function

Private   Function   HexStr2Byte(strSrc   As   String)   As   Byte()
Dim   strLen             As   Integer
Dim   btLen               As   Integer
Dim   I                       As   Integer
Dim   bt()                 As   Byte
        strLen   =   Len(strSrc)
        btLen   =   strLen   /   2
        ReDim   bt(btLen   -   1)
        For   I   =   0   To   btLen   -   1
                bt(I)   =   Val( "&H "   &   Mid$(strSrc,   I   +   I   +   1,   2))
        Next   I
        HexStr2Byte   =   bt
End   Function

Copyright © 2024 严武
Powered by .NET 9.0 on Kubernetes