Conv-Decimal to Base
From Vectorlab
Converts a decimal number to a given base returning a string. See notes at the end.
{ *********************************************** } { Convert from decimal to base "b" } FUNCTION Dec2BaseConverter(n: LONGINT; b, countOfBits: INTEGER ): STRING; VAR temp_l : LONGINT; temp_s : DYNARRAY OF CHAR; BEGIN temp_s := ''; WHILE n/b <> 0 DO BEGIN temp_l := n MOD b; temp_s := Concat(Num2Str(0, temp_l), temp_s ); n := n DIV b; END; { now the binary could be shorter than wished for number of digits, add missing zeros } WHILE Len(temp_s) < countOfBits DO temp_s := Concat('0', temp_s); Dec2BaseConverter := temp_s; END;
- NOTE
- Result here is a string, otherwise can be a LONGINT, but a string is easier to parse for further use.
- Use REAL to avoid LONGINT problem: result is easily longer as 10 digits.
- Really big numbers (> 999999999) resolving in more than 255 chars are rounded, because functions cannot deliver Dynarray of chars.
Categories: Subroutines - Converters | VectorScript Subroutines
VectorScript
VectorScript
VectorScript > VectorScript Example Library
VectorScript > VectorScript Example Library > VectorScript Subroutines
VectorScript > VectorScript Example Library > VectorScript Subroutines > Subroutines - Converters
