Created
July 6, 2011 07:28
-
-
Save slashmili/1066742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION hex2dec(text) | |
RETURNS bigint LANGUAGE SQL AS | |
$$ | |
SELECT sum(digit * 16 ^ (length($1)-pos)) ::bigint | |
FROM (SELECT case | |
when digit between '0' and '9' then ascii(digit) - 48 | |
when digit between 'A' and 'F' then ascii(digit) - 55 | |
end, | |
pos as i | |
FROM (SELECT substring(c from x for 1), x | |
FROM (values(upper($1))) as a(c), | |
generate_series(1,length($1)) as t(x)) | |
as u(digit, pos) | |
) as v(digit, pos); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment