X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9823252BF9 for ; Fri, 19 Aug 2005 11:57:44 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 31597-07 for ; Fri, 19 Aug 2005 14:57:41 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 13BC752B8C for ; Fri, 19 Aug 2005 11:57:40 -0300 (ADT) Received: (qmail 12589 invoked by uid 500); 19 Aug 2005 14:59:35 -0000 Date: Fri, 19 Aug 2005 09:59:35 -0500 From: Bruno Wolff III To: Simon Kinsella Cc: pgsql-sql@postgresql.org Subject: Re: PGSQL function for converting between arbitrary numeric bases? Message-ID: <20050819145935.GB11542@wolff.to> Mail-Followup-To: Bruno Wolff III , Simon Kinsella , pgsql-sql@postgresql.org References: <20050815154850.11963529D9@svr1.postgresql.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050815154850.11963529D9@svr1.postgresql.org> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200508/172 X-Sequence-Number: 22400 On Mon, Aug 15, 2005 at 16:48:40 +0100, Simon Kinsella 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).