Consultor Eletrônico



Kbase 15936: 4GL example to convert BINARY value to DECIMAL
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/05/1998
4GL example to convert BINARY value to DECIMAL

Below is the 4GL code which takes a binary number, represented as a
string, as an input parameter and outputs the number converted to
decimal.

/*input string which represents binary number*/
DEFINE INPUT PARAMETER str AS CHARACTER.
/* output base-10 number */
DEFINE OUTPUT PARAMETER answer AS INTEGER.
/* position in string */
DEFINE VARIABLE count AS INTEGER.
/* length of string */
DEFINE VARIABLE len AS INTEGER.


/* start at the right of the string and if the char is a 1 then raise
2 to the power of the length - count... decrement count */


len = LENGTH(str).
count = len.

DO WHILE count > 0:
IF SUBSTRING(str,count,1) = "1" THEN
DO:
answer = answer + EXP(2,(len - count)).
count = count - 1.
END.
ELSE
count = count - 1.
END.

Progress Software Technical Support Note # 15936