Re: hexadecimal to decimal

From: Joe Conway <mail(at)joeconway(dot)com>
To: Claudio Lapidus <clapidus(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: hexadecimal to decimal
Date: 2003-07-30 21:49:47
Message-ID: 3F283D7B.5050308@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-patches

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bjørn T Johansen 2003-07-30 21:53:09 Upgrading to 7.3.4?
Previous Message DeJuan Jackson 2003-07-30 21:13:55 Re: Minimal system (was Re: Basic questions before start)

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2003-07-30 22:22:55 Re: [PATCHES] [PATCH] Re: Why READ ONLY transactions?
Previous Message Sean Chittenden 2003-07-30 21:40:05 Re: [PATCH] Re: [pgsql-advocacy] Why READ ONLY transactions?