VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expa

by Trent Jackson (1 Submission)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 17th May 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expanded to allow for 10, 12, or even higher

API Declarations


'8 BIT Decimal To Binary String Calc


Rem By Trent Jackson 17/05/06
Rem v0.1 using Visual Basic 5

Rem Free to use & learn from

'decs
Dim IntBuff, CheckSum As Integer
Dim BinVal, StrBuff, DecVal As String

Rate Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expa




'----------
PromptUser:
'----------

'get dec val from user
            DecVal = InputBox("Decimal num to convert?", "Input")

'------------------------------
'ensure that it's a valid input
'------------------------------

'check length
If Len(DecVal) > 3 Then
            
'tell user that the string has exceeded max length
            IntBuff = MsgBox("8 BIT conversion only, max of three digits allowed", _
            vbOKOnly, "Dec to Bin Calc")

'start again
            GoTo PromptUser

Else

'make sure no other chr's except 0-9 exist

'scan string and compare with check sum
For IntBuff = 1 To Len(DecVal)

            StrBuff = Mid$(DecVal, IntBuff, 1)
            
For CheckSum = 48 To 57

'compare val with checksum ascii chr's 0-9
            If StrBuff = Chr(CheckSum) Then GoTo ChrOK 'match?, check next chr

Next CheckSum
            
'read entire string input with no invalid chr's found?
            If IntBuff > Len(DecVal) Then GoTo ConvertToBin

'tell user that the string has an invalid chr
            IntBuff = MsgBox("Invalid character, 0-9 only allowed", _
            vbOKOnly, "Dec to Bin Calc")

'start again since the input value was invalid
            GoTo PromptUser

'check next
ChrOK:      Next IntBuff

End If

'------------
ConvertToBin:
'------------

'---------------------------------------------
Rem BIT weight; 128 64 32 16 8 4 2 1
Rem             MSB--------------LSB
'---------------------------------------------
Rem 1 = set, true, 1
Rem 0 = not, false 0
'---------------------------------------------

'convert the aquired decimal value to integer
            Static CheckByte As Integer
            
'copy val to check byte
            CheckByte = DecVal

'null output bin string first
            BinStr = "00000000"

'test with LSB first
            IntBuff = 1

'use AND operartor to check which bits are on
Do While IntBuff <> 256 'loop until all 8 bits have been compared with

'select case is much more efficient than using IF's
Select Case CheckByte And IntBuff

Case 1
            Mid$(BinStr, 8, 1) = "1"

Case 2
            Mid$(BinStr, 7, 1) = "1"

Case 4
            Mid$(BinStr, 6, 1) = "1"

Case 8
            Mid$(BinStr, 5, 1) = "1"

Case 16
            Mid$(BinStr, 4, 1) = "1"

Case 32
            Mid$(BinStr, 3, 1) = "1"

Case 64
            Mid$(BinStr, 2, 1) = "1"

Case 128
            Mid$(BinStr, 1, 1) = "1"

End Select

            IntBuff = IntBuff * 2 'inc bit weight for comparision

Loop

'notify user with results
            IntBuff = MsgBox("Decimal value of " + DecVal + " = '" + BinStr + _
            "'" + " in binary" + _
            Chr$(13) + Chr$(13) + "Convert another value?." _
            , vbYesNo, "Dec to Bin Calc")

'user wants to convert another value?
If IntBuff = vbYes Then
            
            GoTo PromptUser
         
         Else

            End

End If

End Sub


Download this snippet    Add to My Saved Code

Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expa Comments

No comments have been posted about Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expa. Why not be the first to post a comment about Decimal to binary calc. This one works. 8 BIT BCD output string from dec value input. Could be expa.

Post your comment

Subject:
Message:
0/1000 characters