Padded Binary String
#1
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.

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
Reply
#2
Code: (Select All)
'this allows a negative value padded by zeroes after sign
'set "numdig" to negative value to convert "num" to hexadecimal
'eg. "numdig = -6" to convert "num" to 6-digit hexadecimal string
FUNCTION Zeroes$ (num AS LONG, numdig AS INTEGER)
    STATIC b$, hx AS _BYTE, sg AS _BYTE, numd AS INTEGER, numi AS LONG, v AS LONG
    numi = num: numd = numdig
    IF numi < 0 THEN sg = -1: numi = numi * -1
    IF numd < 0 THEN hx = 1: numd = numd * -1 ELSE hx = 0
    IF hx THEN
        b$ = HEX$(numi)
    ELSE
        b$ = LTRIM$(STR$(numi))
    END IF
    v = numd - LEN(b$)
    IF v > 0 THEN b$ = STRING$(v, 48) + b$
    IF sg = -1 THEN b$ = "-" + b$
    Zeroes$ = b$
END FUNCTION

I needed it even more with hexadecimal numbers.
Reply
#3
Nice.
Reply
#4
I had to write this function because it's near impossible to come up with a near-equivalent to "printf()" from C, because it could take at least two parameters. It became necessary to pad with zeroes at the front of an integer, such as giving serial numbers as filenames and making sure every single filename was the same number of characters.

Should have been able to do that "PRINT USING" trick saving to a temporary file only to enter its contents into a string, to pad an integer with zeroes, but not just with asterisks or spaces. :/
Reply
#5
(08-25-2022, 01:56 AM)mnrvovrfc Wrote: I had to write this function because it's near impossible to come up with a near-equivalent to "printf()" from C, because it could take at least two parameters. It became necessary to pad with zeroes at the front of an integer, such as giving serial numbers as filenames and making sure every single filename was the same number of characters.

Should have been able to do that "PRINT USING" trick saving to a temporary file only to enter its contents into a string, to pad an integer with zeroes, but not just with asterisks or spaces. :/

Sounds like what you wanted to do was something similar to this?  

Code: (Select All)
Dim ss(8) As String
ss(1) = "One"
ss(2) = "Two"
ss(3) = "Three"
ss(4) = "Four"
ss(5) = "Five"
ss(6) = "Six"
ss(7) = "Seven"
ss(8) = "Eight"

Color 4, 1 'so we can see our spaces

For i = 1 To 8
    Print PadBefore(ss(i), 8, "*"), PadAfter(ss(i), 8, "0")
Next


Function PadBefore$ (num$, digits, padding$)
    p$ = num$
    temp$ = String$(digits, padding$)
    Mid$(temp$, digits - Len(p$) + 1) = p$
    PadBefore$ = temp$
End Function

Function PadAfter$ (num$, digits, padding$)
    p$ = num$
    temp$ = String$(digits, padding$)
    Mid$(temp$, 1) = p$
    PadAfter$ = temp$
End Function
Reply
#6
You're good a padding. How about pubtracting?

Oh right, pub-tractoring. Sorry!

Pete

- Never let a farm pun go to waist.
Reply
#7
(08-25-2022, 04:53 AM)Pete Wrote: You're good a padding. How about pubtracting?

Oh right, pub-tractoring. Sorry!

Pete

- Never let a farm pun go to waist.

I was drunk and once drove my tractor into a pond.  That was an attempt at sub-tractoring!
Reply
#8
I hope you remembered to bale before you hit the water!

Pete
Reply
#9
Code: (Select All)
bn$ = _BIN$(whateverNumber) 'the binary string returned by _BIN$
dw = 8 'desired field width

IF LEN(bn$) >= dw THEN
    PRINT bn$ 'number already in desired width or even longer
ELSE
    PRINT RIGHT$(STRING$(dw, "0") + bn$, dw) 'left padded with zeros
END IF
Reply
#10
Heart 
Thank you to Steve and RhoSigma for the elegant solutions.
Reply




Users browsing this thread: 2 Guest(s)