However, if I were to translate a sequential decimal ( base 10 ) number to base 62, I now have 62 x 62 = 3,844 possible values ( 0 - zz ). Now obviously for base 62, the value will be case sensitive. If you can't guarantee case sensitivity, then you'll have to use base 36.
Convert from Base 10 to Base 62 in PL/SQL
CREATE OR REPLACE FUNCTION f_to_base62( a_number_to_convert INTEGER ) RETURN VARCHAR2 ISConvert from Base 62 to Base 10 in PL/SQL
v_modulo INTEGER;
v_temp_int INTEGER := a_number_to_convert;
v_temp_val VARCHAR2(256);
v_temp_char VARCHAR2(1);
c_base62_digits CONSTANT VARCHAR2(62) := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
BEGIN
IF ( a_number_to_convert = 0 ) THEN
v_temp_val := '0';
END IF;
WHILE ( v_temp_int <> 0 ) LOOP
v_modulo := v_temp_int MOD 62;
v_temp_char := SUBSTR( c_base62_digits, v_modulo + 1, 1 );
v_temp_val := v_temp_char || v_temp_val;
v_temp_int := floor(v_temp_int / 62);
END LOOP;
RETURN v_temp_val;
END;
CREATE OR REPLACE FUNCTION f_from_base62( a_value_to_convert VARCHAR2 ) RETURN INTEGER IS
v_iterator INTEGER;
v_length INTEGER;
v_temp_char VARCHAR2(1);
v_temp_int INTEGER;
v_return_value INTEGER := 0;
v_multiplier INTEGER := 1;
v_temp_convert_val VARCHAR2(256) := a_value_to_convert;
c_base62_digits CONSTANT VARCHAR2(62) := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
BEGIN
v_length := length( v_temp_convert_val );
v_iterator := v_length;
WHILE ( v_iterator > 0 ) LOOP
v_temp_char := SUBSTR( v_temp_convert_val, v_iterator, 1 );
v_temp_int := INSTR( c_base62_digits, v_temp_char ) - 1;
v_return_value := v_return_value + ( v_temp_int * v_multiplier );
v_multiplier := v_multiplier * 62;
v_iterator := v_iterator - 1;
END LOOP;
RETURN v_return_value;
END;
2 comments:
Thanks for sharing.
I needed a MySQL equivalent, so have converted your code to suit:
http://www.smiffysplace.com/base-conversions-in-mysql
Very useful, thanks.
I've converted them to T-SQL here Convert between base 10 and base 62 in T-SQL
Post a Comment