Thread: Help with converting hexadecimal to decimal
Hi All, Is there a built in function which works exactly the opposite way as to_hex(). I basically want to convert a a hexadecimal to a decimal. Thanks
Chandra Sekhar Surapaneni wrote: > Hi All, > Is there a built in function which works exactly the opposite way as > to_hex(). > I basically want to convert a a hexadecimal to a decimal. Sure: test=> SELECT x'10'::integer; int4 ------ 16 (1 row) -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
Bruce Momjian <pgman@candle.pha.pa.us> writes: > Chandra Sekhar Surapaneni wrote: > > Hi All, > > Is there a built in function which works exactly the opposite way as > > to_hex(). > > I basically want to convert a a hexadecimal to a decimal. > > Sure: > > test=> SELECT x'10'::integer; > int4 > ------ > 16 > (1 row) 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.
On 31 Mar 2005 21:22:12 -0600, Doug Quale <quale1@charter.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
That's exactly what I am looking for. Thank you Dawid and everyone for your help. Regards chandu -----Original Message----- From: Dawid Kuroczko [mailto:qnex42@gmail.com] Sent: Friday, April 01, 2005 3:28 AM To: Doug Quale Cc: pgsql-general@postgresql.org Subject: Re: [GENERAL] Help with converting hexadecimal to decimal On 31 Mar 2005 21:22:12 -0600, Doug Quale <quale1@charter.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 ---------------------------(end of broadcast)--------------------------- TIP 7: don't forget to increase your free space map settings