Thread: PGSQL function for converting between arbitrary numeric bases?

PGSQL function for converting between arbitrary numeric bases?

From
"Simon Kinsella"
Date:
Hello,

I'm looking - without luck so far - for a PGSQL function for converting
numbers between two arbitrary bases (typically base 10,16 and 26 in my
case).  Something similar to the C 'strtol' function or, ideally, PHP's
baseconvert(string,frombase,tobase) function.

I've search the docs, lists and various other forums but so far no luck.
Just wondered if I've missed anything - all suggestions gratefully received.

Many thank,

Simon K




Re: PGSQL function for converting between arbitrary numeric bases?

From
Bruno Wolff III
Date:
On Mon, Aug 15, 2005 at 16:48:40 +0100, Simon Kinsella <simon@bluefiresystems.co.uk> wrote:
> Hello,
> 
> I'm looking - without luck so far - for a PGSQL function for converting
> numbers between two arbitrary bases (typically base 10,16 and 26 in my
> case).  Something similar to the C 'strtol' function or, ideally, PHP's
> baseconvert(string,frombase,tobase) function.
> 
> I've search the docs, lists and various other forums but so far no luck.
> Just wondered if I've missed anything - all suggestions gratefully received.

I think you are looking at this wrong. You aren't really converting the
numbers. What base is used is only relevant during input and output.
You can write your own input and output functions based on the code used
for the standard input an output functions. There is already a to_hex
function that will convert an integer to text using hexadecimal notation.
For input from hex you might be able to use bit string constants
(e.g. x'10'::int).