Converter Package

Overview

This package contains a set of functions which convert numbers between different bases. The functions cope with any base up to and including base 36.

The package expects numbers passed to it to be positive integers, any sign and/or decimal part will be removed before conversion begins. Conversely, no assumptions are made about particular bits marking a number as negative – all numbers are converted to their positive equivalents.

Decimal values are expressed and accepted as NUMBERs, other bases use data type VARCHAR2. This is because, for bases in excess of base 10, capital letters are used as digits (This is why the upper limit is base 36: 10 digits + 26 letters).

Function Definitions

basen_to_dec (number, base)
Converts a number (held in a character string) from the specified base into its decimal equivalent. If any unexpected characters (including minus signs & decimal points) are encountered, an “invalid number” exception is raised. If base is more than 36 an “argument out of range” exception is raised.
dec_to_basen (number, base)
Converts a decimal number into the specified base. Typical choices for base would be 2 (binary), 8 (octal) and 16 (hexadecimal). A negative sign or decimal part in either parameter will be ignored. If base is more than 36 an “argument out of range” exception is raised.
basen_to_basem (number, basen, basem)
Converts a number (expressed as a string) from one base to another. The same limits apply as are shown above.

Examples

converter.basen_to_dec (‘100110’,2)
Returns the decimal equivalent of binary number 100110, i.e. 38.
converter.dec_to_basen (123,16)
Returns the number 123 expressed in hexadecimal, i.e. ‘7B’.
converter.basen_to_basem (‘110’,8,2)
Returns the binary version of the octal number 110 (72 decimal), i.e. 1001000.

Download

If the above has whetted your interest, you can download the package building script, oraconv.zip (2,584 bytes). I hope you find it useful.

This code is made available for use in both private and commercial applications under a Creative Commons Licence.

Whilst every effort has been made to ensure these routines operate accurately and properly, no liability will be accepted for any problems they may cause on your system.

Leave a Comment