Re: hexadecimal to decimal - Mailing list pgsql-general

From Joe Conway
Subject Re: hexadecimal to decimal
Date
Msg-id 3F283D7B.5050308@joeconway.com
Whole thread Raw
In response to hexadecimal to decimal  ("Claudio Lapidus" <clapidus@hotmail.com>)
Responses Re: hexadecimal to decimal  (Ron Johnson <ron.l.johnson@cox.net>)
List pgsql-general
Claudio Lapidus wrote:
> I have an attribute in a table which stores hexadecimal numbers as a
> two-character string, i.e. the attr definition is char(2). Now I need to
> display these values in decimal, but I wasn´t able to find such a function.
> So, is there a way to perform this conversion?
>

I would have thought there was an easier way (I couldn't think of it),
but this seems to work:

create or replace function hex_to_int(char(2)) returns integer as '
declare
  v_ret record;
begin
  for v_ret in execute ''select x'''''' || $1 || ''''''::int as f'' loop
    return v_ret.f;
  end loop;
end;
' language 'plpgsql';

create table foo(f1 char(2));
insert into foo values ('ff');
insert into foo values ('fe');
insert into foo values ('fd');

regression=# select hex_to_int(f1) from foo;
  hex_to_int
------------
         255
         254
         253
(3 rows)

I'm sure you could do this with plperl or one of the other PLs as well.

HTH,

Joe


pgsql-general by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: Basic questions before start
Next
From: Bjørn T Johansen
Date:
Subject: Upgrading to 7.3.4?