Option ExplicitFunction Bin2Dec(InputData As String) As Double
''
'' This converts Binary to Decimal
''
Dim DecOut As Double
Dim I As Integer
Dim LenBin As Double
Dim JOne As StringLenBin = Len(InputData)''
'' Make sure that it is a Binary Number
''
For I = 1 To LenBin
JOne = Mid(InputData, I, 1)
If JOne <> "0" And JOne <> "1" Then
MsgBox "NOT A BINARY NUMBER", vbCritical
Exit Function
End If
Next I
DecOut = 0
For I = Len(InputData) To 1 Step -1
If Mid(InputData, I, 1) = "1" Then
DecOut = DecOut + 2 ^ (Len(InputData) - I)
End If
Next I
Bin2Dec = DecOut
End FunctionFunction Dec2Bin(InputData As Double) As String
''
'' Converts Decimal to Binary
'' This uses the Quotient Remainder method
''
Dim Quot As Double
Dim Remainder As Double
Dim BinOut As String
Dim I As Integer
Dim NewVal As Double
Dim TempString As String
Dim TempVal As Double
Dim BinTemp As String
Dim BinTemp1 As String
Dim PosDot As Integer
Dim Temp2 As String
'' Check to see if there is a decimal point or not
''
If InStr(1, CStr(InputData), ".") Then
MsgBox "Only Whole Numbers can be converted", vbCritical
GoTo eds
End IfBinOut = ""
NewVal = InputData
DoAgain:'' Start the Calculations off
NewVal = (NewVal / 2)
'' If we have a remainder
If InStr(1, CStr(NewVal), ".") Then
BinOut = BinOut + "1"
'' Get rid of the Remainder
NewVal = Format(NewVal, "#0")
NewVal = (NewVal - 1)
If NewVal < 1 Then
GoTo DoneIt
End If
Else
BinOut = BinOut + "0"
If NewVal < 1 Then
GoTo DoneIt
End If
End If
GoTo DoAgainDoneIt:
'MsgBox BinOut
BinTemp = ""'' Reverse the Result
For I = Len(BinOut) To 1 Step -1
BinTemp1 = Mid(BinOut, I, 1)
BinTemp = BinTemp + BinTemp1
Next IBinOut = BinTemp'' Output the Result
Dec2Bin = BinOut
eds:
End FunctionPublic Function LR(InputData As Integer, Mode As Boolean) As Integer
Dim BufStr As String
Dim TarStr As String
If InputData > 255 Or InputData < 0 Then
MsgBox "Error in Input Data!"
Exit Function
End If
BufStr = Dec2Bin(CDbl(InputData))
If Len(BufStr) < 8 Then BufStr = String(8 - Len(BufStr), "0") + BufStr
If Mode Then ' Left
TarStr = Right(BufStr, 7) + "0"
Else 'Right
TarStr = "0" + Left(BufStr, 7)
End If
LR = CInt(Bin2Dec(TarStr))
End Function'使用
LR(255,True) 255左移一位
LR(255,False) 255右移一位
''
'' This converts Binary to Decimal
''
Dim DecOut As Double
Dim I As Integer
Dim LenBin As Double
Dim JOne As StringLenBin = Len(InputData)''
'' Make sure that it is a Binary Number
''
For I = 1 To LenBin
JOne = Mid(InputData, I, 1)
If JOne <> "0" And JOne <> "1" Then
MsgBox "NOT A BINARY NUMBER", vbCritical
Exit Function
End If
Next I
DecOut = 0
For I = Len(InputData) To 1 Step -1
If Mid(InputData, I, 1) = "1" Then
DecOut = DecOut + 2 ^ (Len(InputData) - I)
End If
Next I
Bin2Dec = DecOut
End FunctionFunction Dec2Bin(InputData As Double) As String
''
'' Converts Decimal to Binary
'' This uses the Quotient Remainder method
''
Dim Quot As Double
Dim Remainder As Double
Dim BinOut As String
Dim I As Integer
Dim NewVal As Double
Dim TempString As String
Dim TempVal As Double
Dim BinTemp As String
Dim BinTemp1 As String
Dim PosDot As Integer
Dim Temp2 As String
'' Check to see if there is a decimal point or not
''
If InStr(1, CStr(InputData), ".") Then
MsgBox "Only Whole Numbers can be converted", vbCritical
GoTo eds
End IfBinOut = ""
NewVal = InputData
DoAgain:'' Start the Calculations off
NewVal = (NewVal / 2)
'' If we have a remainder
If InStr(1, CStr(NewVal), ".") Then
BinOut = BinOut + "1"
'' Get rid of the Remainder
NewVal = Format(NewVal, "#0")
NewVal = (NewVal - 1)
If NewVal < 1 Then
GoTo DoneIt
End If
Else
BinOut = BinOut + "0"
If NewVal < 1 Then
GoTo DoneIt
End If
End If
GoTo DoAgainDoneIt:
'MsgBox BinOut
BinTemp = ""'' Reverse the Result
For I = Len(BinOut) To 1 Step -1
BinTemp1 = Mid(BinOut, I, 1)
BinTemp = BinTemp + BinTemp1
Next IBinOut = BinTemp'' Output the Result
Dec2Bin = BinOut
eds:
End FunctionPublic Function LR(InputData As Integer, Mode As Boolean) As Integer
Dim BufStr As String
Dim TarStr As String
If InputData > 255 Or InputData < 0 Then
MsgBox "Error in Input Data!"
Exit Function
End If
BufStr = Dec2Bin(CDbl(InputData))
If Len(BufStr) < 8 Then BufStr = String(8 - Len(BufStr), "0") + BufStr
If Mode Then ' Left
TarStr = Right(BufStr, 7) + "0"
Else 'Right
TarStr = "0" + Left(BufStr, 7)
End If
LR = CInt(Bin2Dec(TarStr))
End Function'使用
LR(255,True) 255左移一位
LR(255,False) 255右移一位
解决方案 »
- MDI子窗体菜单问题
- 如防止抓屏
- 请问如何用VB来实现论坛消息的发送
- 打包了皮肤控件,别人电脑还是打不开程序
- 怎样区别出在系统区域单击鼠标与单击鼠标右键弹出的系统菜单
- 我以前只有做过asp+access的开发,最近,有机会做做,vb+access,区别很大吗?小弟想学,有什么好书推荐吗?或者电子书?谢谢各位
- 欢迎访问http://CoolSlob.fykj.com/。目前唯一可以查询CSDN FAQ的站点!
- 求vb图象处理的控件,要求:模糊度,光亮度
- 怎样用代码测试A盘中无盘?急!!!
- vb动态控件
- Winsock控件疑问……
- 小弟现在从头学习vb,可惜不知道该看什么书,请各位多多指点。来着有分!·!·!·!·
没有你说的那样简单
>>如何对一个8bit数据实现左移或右移一位的操作?
不知道到底想做什么?