StringBinToValue
Synopsis
value = StringBinToValue(string, optlen)
D0 A0 D0
LONG StringBinToValue(STRPTR, WORD);C Prototype
LONG Bin2Val(STRPTR, LONG);Description
Converts a binary ASCII string (a sequence of 1 and 0 characters) into a decimal LONG value. This is the inverse operation of ValueToStringBin.
The function converts strings of variable length from 1 to 32 characters, thus covering the full 32-bit range.
The following special characters are permitted in the input string:
%— Optional prefix indicator for binary values. Has no effect on conversion.+— Indicates positive conversion (already the default). Has no effect on conversion.-— Performs a logical NOT on the final result, indicating that the NOT of the input string is desired.
Inputs
- string — Pointer to a null-terminated string containing the ASCII binary sequence to convert.
- optlen — By default NULL, which operates on all 32 bits. When specified, it limits the number of bits used for conversion.
Result
- value — The converted LONG value.
Example
; With optlen = NULL:
; string = "000100" --> 4
; string = "%0100" --> 4
; string = "-%100" --> -5
; string = "%-100" --> -5
; string = "-100" --> -5
; With optlen = 4:
; string = "1110010" --> 2
; string = "0010" --> 2
; string = "111111" --> 15Notes
If the - character is present, it always overrides the + character regardless of position.
See Also
StringDecToValue, StringHexToValue, ValueToStringBin
Implementation
The original 68020 assembly implementation:
; (20-Feb-1995) --- value = StringBinToValue(string, optlen) (a0/d0)
_LVOStringBinToValue
movem.l d1-d3/a0,-(sp)
moveq #0,d2 ; Mark Negative
moveq #"0",d3 ; Type Of Chars
B2V_Skp cmp.b (a0),d3 ; Check Jolly Char
ble.s B2V_Con
cmpi.b #"%",(a0)+ ; Jolly...
beq.s B2V_Skp
cmpi.b #"+",-1(a0) ; Pos
beq.s B2V_Skp
cmpi.b #"-",-1(a0) ; NOT... triggers something...
bne.s B2V_Skp
moveq #1,d2 ; Set Mark Negative
bra.s B2V_Skp
B2V_Con move.w d0,d1 ; Forced length provided?
bne.s B2V_Chk ; Yes... use this one...
STRLEN a0,d1 ; Entire string
B2V_Chk moveq #0,d0
subq.w #1,d1
bmi.s B2V_Ext
B2V_SBt cmp.b (a0)+,d3
beq.s B2V_Lp
bset d1,d0
B2V_Lp dbf d1,B2V_SBt
tst.w d2
beq.s B2V_Ext
not.l d0
B2V_Ext movem.l (sp)+,d1-d3/a0
rts