我试图在 MySQL 中将字符串散列为 64 位值(bigint)。我知道 MD5() 函数,它返回一个 128 位哈希作为二进制字符串。我很乐意只取这个结果的底部或顶部 64 位。但是,我无法弄清楚如何从二进制字符串类型转换为任何类型的数字类型。任何指针?
问问题
12681 次
3 回答
16
使用该CONV()
函数将 MD5 哈希从 16 进制转换为 10 进制,CAST
并将其转换为数字:
select cast(conv(substring(md5(id), 1, 16), 16, 10) as unsigned integer) from SomeTable;
于 2009-08-11T10:45:26.740 回答
2
CREATE FUNCTION dbo.HexStrToVarBinary(@hexstr varchar(8000))
RETURNS varbinary(8000)
AS
BEGIN
DECLARE @hex char(1), @i int, @place bigint, @a bigint
SET @i = LEN(@hexstr)
set @place = convert(bigint,1)
SET @a = convert(bigint, 0)
WHILE (@i > 0 AND (substring(@hexstr, @i, 1) like '[0-9A-Fa-f]'))
BEGIN
SET @hex = SUBSTRING(@hexstr, @i, 1)
SET @a = @a +
convert(bigint, CASE WHEN @hex LIKE '[0-9]'
THEN CAST(@hex as int)
ELSE CAST(ASCII(UPPER(@hex))-55 as int) end * @place)
set @place = @place * convert(bigint,16)
SET @i = @i - 1
END
RETURN convert(varbinary(8000),@a)
END
GO
于 2009-08-11T10:45:25.727 回答
2
于 2019-02-14T09:00:14.177 回答