首页 > 代码库 > My Sql 对汉字,字母的hash编码算法

My Sql 对汉字,字母的hash编码算法

DELIMITER $$

CREATE

FUNCTION `jtorder`.`F_GET_HASH_VALUE`(str VARCHAR(500))
RETURNS BIGINT

BEGIN
/*
* @desc:获取字符的HashValue
str:字符
* @author:shiyj
eg:str=abc123
*/
DECLARE result BIGINT;
SET @pos = 1;
SET @hashValue = http://www.mamicode.com/0;
-- 616263313233
SET @szHex = HEX(str);
-- 12
SET @size = LENGTH(@szHex);
WHILE @pos<@size+1 DO
-- 61
SET @cCh = SUBSTRING(@szHex,@pos,2);
-- a的ASCII=97
SET @nCh = CAST(ASCII(UNHEX(@cCh)) AS UNSIGNED);
-- 97+98+99+49+50+51
SET @hashValue = http://www.mamicode.com/@hashValue + @nCh;
SET @pos = @pos + 2;
END WHILE;

SET result = @hashValue;
RETURN result;
END$$

DELIMITER ;

My Sql 对汉字,字母的hash编码算法