欢迎光临:
非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源)
智能多路控制(串口编程开关) 带源码!
05-07
25
字节(Bytes)与位(Bits)转换读写函数
作者:枕善居主 / 查看次数: 7293 / 评论: 3
'
'***********************************
'** 【字节(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
'==================================================================================================================
'***********************************
'** 【字节(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
'==================================================================================================================
后两个函数,写入和读取某位不用这样麻烦吧,且效率较底,直接用位运算符就可以搞定了啊。[smile]
每个字节由高低共八位组成,这是对字节进行位操作的四个函数,能够对字节的位进行直接读写。VB中没有提供位操作函数,所以把它贴出来供各位VB爱好者使用,
大家也可以象VC一样方便的进行位操作了。
大家也可以象VC一样方便的进行位操作了。
发表评论
您没有权限发表评论!
上一篇
下一篇
相关日志:
文章来自:
Tags:
评论: 3 |
回复


可以用乘除“2^x”形式的常数来实现移位,VB编译器会自动优化成移位指令
至于 检测/设置/反转 位,用And/Or/Xor