Re: Help with converting hexadecimal to decimal

From: Dawid Kuroczko <qnex42(at)gmail(dot)com>
To: Doug Quale <quale1(at)charter(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Help with converting hexadecimal to decimal
Date: 2005-04-01 09:28:15
Message-ID: 758d5e7f05040101284f594649@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 31 Mar 2005 21:22:12 -0600, Doug Quale <quale1(at)charter(dot)net> wrote:
> > > I basically want to convert a a hexadecimal to a decimal.
> > Sure:
> >
> > test=> SELECT x'10'::integer;
> > int4
> > ------
> > 16
> > (1 row)

Oh, some time ago I was looking for such a function and somehow
I missed that. Maybe there should be a hint about it around
string/numeric functions?

> That's not the inverse of to_hex(). to_hex() takes an integer and
> returns text. You've taken the bitstring constant B'10000' and
> coerced it to integer. The inverse funtion would take the text '10'
> and return the integer 16.

Well, you can make a function out of it, say:

CREATE FUNCTION from_hex(t text) RETURNS integer AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN EXECUTE 'SELECT x'''||t||'''::integer AS hex' LOOP
RETURN r.hex;
END LOOP;
END
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

Regards,
Dawid

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Philippe Lang 2005-04-01 09:28:26 PL/PERL with composite return values & PGSQL 7.4?
Previous Message Shaun Clements 2005-04-01 09:20:33 : Postgres IX