有这样的数据:  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)也可以。

解决方案 »

  1.   

    你那是16进制数据,不是二进制。mysql> select CONV('12345678', 16, 10);
    +--------------------------+
    | CONV('12345678', 16, 10) |
    +--------------------------+
    | 305419896                |
    +--------------------------+
    1 row in set (0.00 sec)mysql>
      

  2.   

    CONV这是处理已经转换为16进制显示的字符吧。
    我的要求是要把16进制数据转换为数值
      

  3.   

    还是自己解决吧:
    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扩展函数来处理。