Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Public Function Binary2Deci(3DigiBinary As String) As Long
Select Case valtoconv
Case "000"
Bin2Comp_Conv = 0
Case "001"
Bin2Comp_Conv = 1
Case "010"
Bin2Comp_Conv = 2
Case "011"
Bin2Comp_Conv = 3
Case "100"
Bin2Comp_Conv = -4
Case "101"
Bin2Comp_Conv = -3
Case "110"
Bin2Comp_Conv = -2
Case "111"
Bin2Comp_Conv = -1
End Select
End Function
Private Sub Command1_Click()
MsgBox TwosCompConvert(Text1.Text, FourBit)
End Sub
Private Sub Form_Load()
Text1.Text = 1000
End Sub
Public Enum EnumBits
ThreeBit = 0
FourBit = 1
EightBit = 2
End Enum
Public Function TwosCompConvert(BinaryString As String, E_Bits As EnumBits) As Long
Dim LargestVal As Long
Dim TempLong As Long
'Determine Largest and Smallest values
Select Case E_Bits
Case ThreeBit
LargestVal = 3
Case FourBit
LargestVal = 7
Case EightBit
LargestVal = 127
End Select
'Test
TempLong = BinToDec(BinaryString)
If TempLong > LargestVal Then
'Produce Negative Result
TwosCompConvert = BinToNegDec(TempLong, BinaryString)
Else
'Result is Positive
TwosCompConvert = TempLong
End If
End Function
Function BinToDec(value As String) As Long
'Function by Francesco Balena
Dim result As Long, i As Integer, exponent As Integer
For i = Len(value) To 1 Step -1
Select Case Asc(Mid$(value, i, 1))
Case 48 ' "0", do nothing
Case 49 ' "1", add the corresponding power of 2
result = result Or Power2(exponent)
Case Else
Err.Raise 5 ' Invalid procedure call or argument
End Select
exponent = exponent + 1
Next
BinToDec = result
End Function
Function BinToNegDec(NumToSubtractFrom As Long, value As String) As Long
'Function inspired by Francesco Balena
'Loop from left to right subtracting from NumToSubtractFrom as you go
Dim result As Long
Dim i As Integer
Dim exponent As Integer
exponent = Len(value)
For i = 1 To Len(value)
Select Case Asc(Mid$(value, i, 1))
Case 48 ' "0", do nothing
Case 49 ' "1", add the corresponding power of 2
NumToSubtractFrom = NumToSubtractFrom Or NumToSubtractFrom - Power2(exponent)
Case Else
Err.Raise 5 ' Invalid procedure call or argument
End Select
exponent = exponent - 1
Next
BinToNegDec = NumToSubtractFrom
End Function
Function Power2(ByVal exponent As Long) As Long
'Function by Francesco Balena
Static res(0 To 31) As Long
Dim i As Long
' rule out errors
If exponent < 0 Or exponent > 31 Then Err.Raise 5
' initialize the array at the first call
If res(0) = 0 Then
res(0) = 1
For i = 1 To 30
res(i) = res(i - 1) * 2
Next
' this is a special case
res(31) = &H80000000
End If
' return the result
Power2 = res(exponent)
End Function
Sub negbin2dec()
Dim BinString As String, temp As String
Dim DecimalNum As Long, i As Integer, j As Integer
binaer = Selection.Text
dezi = 0
j = 2
For i = Len(BinString) - 1 To 1 Step -1
DecimalNum = DecimalNum + (Mid(BinString, j, 1) * (2 ^ i))
j = j + 1
Next i
If Left(BinString, 1) = 1 Then
temp = "-" & CStr(DecimalNum)
DecimalNum = CLng(temp)
End If
End Sub
[b][navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell[/b]
Sub negbin2dec()
Dim BinString As String, temp As String
Dim DecimalNum As Long, i As Integer, j As Integer
binaer = Selection.Text
dezi = 0
j = 2
For i = Len(BinString) - 1 To 1 Step -1
DecimalNum = DecimalNum + (Mid(BinString, j, 1) * (2 ^ i))
j = j + 1
Next i
If Left(BinString, 1) = 1 Then
temp = "-" & CStr(DecimalNum)
DecimalNum = CLng(temp)
End If
End Sub
Private Function Hex2Bin(hx As String) As String
Select Case hx
Case "0": Hex2Bin = "0000": Case "1": Hex2Bin = "0001"
Case "2": Hex2Bin = "0010": Case "3": Hex2Bin = "0011"
Case "4": Hex2Bin = "0100": Case "5": Hex2Bin = "0101"
Case "6": Hex2Bin = "0110": Case "7": Hex2Bin = "0111"
Case "8": Hex2Bin = "1000": Case "9": Hex2Bin = "1001"
Case "A": Hex2Bin = "1010": Case "B": Hex2Bin = "1011"
Case "C": Hex2Bin = "1100": Case "D": Hex2Bin = "1101"
Case "E": Hex2Bin = "1110": Case "F": Hex2Bin = "1111"
End Select
End Function
Dim temp As Long
temp = Val("&H" & myinputpair)
If temp > 127 Then temp = temp - 256
myoutputint = temp