Convert hex to binary in MySQL

前端 未结 2 1306
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 17:51

Currently I search a function in MySQL to do conversion between hex string to binary representation, example:

0000 -> 0000000000000000
00AA -> 00000000         


        
2条回答
  •  庸人自扰
    2021-01-01 18:18

    Use CONV() function:

    CONV(string, 16, 2)
    

    To have length according to input:

    LPAD(CONV(string, 16, 2), LENGTH(string)*4, '0')
    

    As CONV() works with 64-bit precision, you can't have more than 64 bits converted, so you can use this as well:

    LPAD(CONV(string, 16, 2), 64, '0')
    

    and you should check that LENGTH(string) <= 16 or you may get erroneous results.

提交回复
热议问题