471,108 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

Casting hexadecimal to int

I was looking for a way to cast hex numbers (stored in a varchar column)
to int. I found many people asking the same thing, but no answers.

It seems that this is supported by the following syntax

select x'1f'::int;

Since this doesn't work with variable values, I wrote this following
function. Perhaps it will help anyone wanting to do the same thing.

CREATE OR REPLACE FUNCTION hex_to_int(varchar) RETURNS integer AS '
DECLARE
h alias for $1;
exec varchar;
curs refcursor;
res int;
BEGIN
exec := ''SELECT x'''''' || h || ''''''::int'';
OPEN curs FOR EXECUTE exec;
FETCH curs INTO res;
CLOSE curs;
return res;
END;'
LANGUAGE 'plpgsql'
IMMUTABLE
STRICT;

--
Richard van den Berg, CISSP

Trust Factory B.V. | http://www.trust-factory.com/
Bazarstraat 44a | Phone: +31 70 3620684
NL-2518AK The Hague | Fax : +31 70 3603009
The Netherlands |
Jul 19 '05 #1
0 5759

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by James DeClerk | last post: by
10 posts views Thread by pavithra.eswaran | last post: by
5 posts views Thread by Damon | last post: by
1 post views Thread by Fernando Barsoba | last post: by
15 posts views Thread by jaks.maths | last post: by
8 posts views Thread by Vijay | last post: by
7 posts views Thread by elliotng.ee | last post: by
5 posts views Thread by Just D | last post: by
6 posts views Thread by Andrea | last post: by
6 posts views Thread by sweeet_addiction16 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.