suve

awful: function: convert-base

Available since rev.40 (v.0.5.4).

Declaration

ascii :convert-base ( string $NUMBER , int $FROM , int $TO )

Summary

Converts the provided number from base-$FROM to base-$TO. Specified bases must be in [2, 36] range. If omitted, they default to 10.

Symbols representing digits that lie outside the base-from are silently ignored.

Note that arguments are always converted to string if they are of different type, which can lead to interesting results, e.g. if one provides a bin number, and then uses something else than 2 as the base-from.

Example

# :convert-base example
 
:set &org s''
:set &from &to i0
 
:write s'Write number: '
:readln &org
 
:write s'Write original base: '
:read &from
 
:write s'Write destination base: '
:read &to
 
:set &org :convert-base $org $from $to
:writeln sxConverted number: x $org
 
:set &org :convert-base $org $to $from
:writeln sxReverse conversion: x $org

wikipage modified on 2014/0916/1542