数据载入中,请稍等...
    数据载入中,请稍等...
博客公告
    数据载入中,请稍等...
时间记忆
    数据载入中,请稍等...
博客登陆
最新日志
    数据载入中,请稍等...
最新评论
    数据载入中,请稍等...
最新留言
    数据载入中,请稍等...
博客相册
博客好友
    数据载入中,请稍等...
友情连接
博客统计
    数据载入中,请稍等...
字节(Bytes)与位(Bits)转换读写函数 | 2007-4-25 21:57:00
***********************************
'** 【字节(Bytes)与位(Bits)转换读写函数】**
'***********************************
'
'==================================================================================================================
'字节(Byte十进制值)转换为8位二进制
'调用示例:Debug.Print AxBin(42) = "00101010"
Private Function AxBin(byteBin As Byte) As String
          Dim X As Double
          Dim str1 As String
          
          X = CByte(byteBin)
          
          Do
              str1 = (X And 1) & str1
              X = X \ 2
          Loop While X

          If Len(str1) > 8 Then str1 = Left$(str1, 8)
          
          Do Until Len(str1) = 8  '补足8位
               str1 = 0 & str1
          Loop
          
          AxBin = str1
          
End Function
'==================================================================================================================
'8位二进制转换为字节(Byte),即十进制值
'调用示例:Debug.Print OxBin("00101010") = 42
Private Function OxBin(strBin As String) As Byte
    Dim iCount As Long
    Dim tmpVal As Double
    Dim tmpV As String
    
    tmpV = Trim$(strBin)
    
    '每字节是8位
    If Len(tmpV) > 8 Then
       tmpV = Left$(tmpV, 8)
    ElseIf Len(tmpV) < 8 Then
       Do Until Len(tmpV) = 8  
          tmpV = 0 & tmpV
       Loop
    End If

    For iCount = 1 To Len(tmpV)  '下面可是重点:实现2^7+2^6+2^5+2^4+2^3+2^1+2^0
        tmpVal = Val(tmpVal) + Val(Mid$(tmpV, iCount, 1)) * (2 ^ (Len(tmpV) - iCount))
    Next iCount  'val()返回数值,mid$()从一个字符串中返回指定的字符 icount是设置从第几个字符开始,1是指获取1个字符    
    
    OxBin = CByte(tmpVal)
    
End Function
'==================================================================================================================
'读取字节(bByte)的第(bNum)位的值,bNum范围:1-8 之间
'调用示例:Debug.Print ReadByteX(42, 5) = 1
Public Function ReadByteX(bByte As Byte, bNum As Integer) As Integer   '结果为1、0
    Dim str1 As String
    
    '转换成位
    str1 = AxBin(bByte)
    
    'bNum范围:1-8 之间
    If bNum = 0 Then bNum = 1
    If bNum > 8 Then bNum = 8
    
    '求值
    str1 = Mid$(str1, bNum, 1)
    ReadByteX = CLng(str1)
    
End Function
'==================================================================================================================
'写入字节(bByte)的第(bNum)位的值(bInt1),bNum范围:1-8 之间,bInt1范围:0-1 之间
'调用示例:Debug.Print ReadByteX(42, 5, 0) = 34
Public Function WriteByteX(bByte As Byte, bNum As Integer, bInt1 As Integer) As Byte
    Dim i As Integer
    Dim str1 As String, str2 As String
    
    '转换成位
    str1 = AxBin(bByte)
    
    'bNum范围:1-8 之间
    If bNum = 0 Then bNum = 1
    If bNum > 8 Then bNum = 8
    
    'bInt1范围:0-1 之间
    If bInt1 < 0 Then bInt1 = 0
    If bInt1 > 1 Then bInt1 = 1

    '写入字节的第(bNum)位的值(bInt1)
    For i = 1 To 8
        If i <> bNum Then
           str2 = str2 & Mid$(str1, i, 1)
        Else
           str2 = str2 & CStr(bInt1)
        End If
    Next
    
    '转换成新的字节(Byte)
    WriteByteX = OxBin(str2)
    
End Function
发表评论:
数据载入中,请稍等...

超音速工作室 版权所有