Convert number into base 36 string
Anyone has any idea why this (see code below) does not work? I'm simply trying to convert a number into a base-36 string. The "panic" variable is not part of the original code but don't remove it since my function goes into an infinite loop without it!
========================
DROP FUNCTION PRINTBASE36;
DELIMITER //
CREATE FUNCTION PRINTBASE36(num BIGINT)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE panic, newnum, nextnum BIGINT;
DECLARE stream VARCHAR(255);
SET panic = 0;
SET stream = '';
SET newnum = num;
WHILE newnum > 0 DO
SET panic = panic + 1;
IF panic > 1000 THEN
RETURN 'Failed... Infinite loop!';
END IF;
SET nextnum = newnum % 36;
SET stream = CONCAT(stream, ORD(newnum - (nextnum * 36)));
SET newnum = nextnum;
END WHILE;
RETURN REVERSE(stream);
END;
//
delimiter ;
select PRINTBASE36(55);
Subject
Views
Written By
Posted
Convert number into base 36 string
14715
March 21, 2006 11:22PM
3671
March 22, 2006 02:38PM
2950
June 23, 2006 12:10PM
5992
June 23, 2006 11:10AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.