My use case for this was to convert hexadecimal SHA-1 hash values ββto integers. Without attention to numerical accuracy in the entire function, this use case easily reveals flaws; but of course, use cases with an even larger "very large number" are easy to identify.
Given some SHA-1 hashes, the first of the response decisions made led to the following:
SELECT hex_to_int('356e90d2a2d414ba8757ec2ab91f2f19c481d4c3'); -- returns 305042208670409000000000000000000000000000000000 SELECT hex_to_int('aaa9f7193cc8efe7e98145b0f8d9ae5f1712c25b'); -- returns 974318782301086000000000000000000000000000000000
Of course, every result that has the correct location with so many zeros is a sign of inadequate accuracy for use.
This is where I ended up to get the accuracy I need:
CREATE OR REPLACE FUNCTION hex_to_int(hexVal varchar) RETURNS numeric(1000) AS $$ DECLARE intVal numeric(1000) := 0; hexLength integer; i integer; hexDigit varchar; BEGIN hexLength := length(hexVal); FOR i IN 1..hexLength LOOP hexDigit := substr(hexVal, hexLength - i + 1, 1); intVal := intVal + CASE WHEN hexDigit BETWEEN '0' AND '9' THEN CAST(hexDigit AS numeric(1000)) WHEN upper(hexDigit) BETWEEN 'A' AND 'F' THEN CAST(ascii(upper(hexDigit)) - 55 AS numeric(1000)) END * CAST(16 AS numeric(1000)) ^ CAST(i - 1 AS numeric(1000)); END LOOP; RETURN intVal; END; $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; SELECT hex_to_int('356e90d2a2d414ba8757ec2ab91f2f19c481d4c3');
source share