有这样的数据: 0x1234567890abcdef的二进制数据
我要取前4个字节,转换为整数
mysql> SELECT convert(substring(0x1234567890abcdef,1,4), SIGNED );
+-----------------------------------------------------+
| convert(substring(0x1234567890abcdef,1,4), SIGNED ) |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
这个代码哪里有问题?
greeneryye(叶浩恩) 2011-02-16 18:58:35
正确的是得到 2018915346(也就是0x78563412)
得到305419896(0x12345678)也可以。
我要取前4个字节,转换为整数
mysql> SELECT convert(substring(0x1234567890abcdef,1,4), SIGNED );
+-----------------------------------------------------+
| convert(substring(0x1234567890abcdef,1,4), SIGNED ) |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
这个代码哪里有问题?
greeneryye(叶浩恩) 2011-02-16 18:58:35
正确的是得到 2018915346(也就是0x78563412)
得到305419896(0x12345678)也可以。
+--------------------------+
| CONV('12345678', 16, 10) |
+--------------------------+
| 305419896 |
+--------------------------+
1 row in set (0.00 sec)mysql>
我的要求是要把16进制数据转换为数值
create function binary2int(buf binary(4))
returns int
begin
return ascii(substring(buf,4,1))<<24
|ascii(substring(buf,3,1))<<16
|ascii(substring(buf,2,1))<<8
|ascii(substring(buf,4,1));end&&用到函数substring,估计效率不高
但能解决问题
后来我又用C写了个mysql扩展函数来处理。