April 26 2011 at 10:10 PM
Moderator

This function is similar to the other one in a few ways, but also different in a few ways.

Like the other one, it accepts two input strings of basically any lengths. Like the other one, it stores the result in the first string.

In this case, the string is not a sequence of decimal numerals in big-endian order, but is a little-endian unsigned integer of arbitrary size.

Note that in this function, as well as in the last, the two input strings can be different lengths. The first one, since it will store the result, is expanded to the size of the larger string, plus 1, to accommodate a possible carry. Afterward, leading zeros are removed. In the case of the other function, 0 becomes "0". In the case of this function, 0 becomes "", an empty string.

Regards,
Michael

'public domain, april 2011, michael calkins
DEFINT A-Z
SUB addbinstr (a AS STRING, b AS STRING)
DIM i AS INTEGER
DIM c AS INTEGER
DIM n AS INTEGER
n = LEN(b) - LEN(a)
IF n < 0 THEN n = 0
a = a + STRING\$(n + 1, &H0)
c = 0
FOR i = 1 TO LEN(a)
IF i > LEN(b) THEN
n = &H0
ELSE
n = ASC(MID\$(b, i, 1))
END IF
n = c + ASC(MID\$(a, i, 1)) + n
IF n AND &H100 THEN
c = 1
n = n AND &HFF
ELSE
c = 0
END IF
MID\$(a, i, 1) = CHR\$(n)
NEXT
FOR i = LEN(a) TO 1 STEP -1
IF ASC(MID\$(a, i, 1)) > &H0 THEN a = LEFT\$(a, i): EXIT FOR
IF i = 1 THEN a = ""
NEXT i
END SUB

 This message has been edited by MCalkins on Apr 26, 2011 10:17 PMThis message has been edited by MCalkins on Apr 26, 2011 10:16 PM

 Respond to this message