08-25-2022, 12:35 AM
(This post was last modified: 08-25-2022, 02:29 AM by James D Jarvis.)
A function to create a padded binary string.
if _bin$ would return "10" this can return "00000010"
EDIT: corrected a typo in function the demo let slip past but using this in another program exposed the error. All good now.
if _bin$ would return "10" this can return "00000010"
EDIT: corrected a typo in function the demo let slip past but using this in another program exposed the error. All good now.
Code: (Select All)
'binpad$
' written with qb64PE 0.8.2
'a function to pad strings for binary values of numbers with leadiing 0's
'
Print "Enter a number and the binary string will be returned"
Do
Input "Enter a number from 0 to 255 (-1 to exit) :", n
If n > -1 And n < 256 Then
bb = n
Print
Print "Integer: "; n
bt$ = binpad$(n, 8)
Print "Binary:"; bt$
_Clipboard$ = bt$ 'write the string to the clipbaord
End If
Loop Until n = -1
Print "BYE."
'===================================================================================
Function binpad$ (b, d)
'binpad$ returns a string of a binary value
'leading 0's will be inserted to pad a string to the minimum size of d characters
'b is the binary value
'd is digits to display
bt$ = _Bin$(b)
If Len(bt$) < d Then
c = d - Len(bt$)
bt$ = String$(c, Asc("0")) + bt$
End If
binpad$ = bt$
End Function