我试图将bigint无符号数的二进制表示存储到MySql中的二进制(8)列中。例如,值9
应存储为0...01001
,并填充一堆0
s的强制性左边距。这可能吗?
答案 0 :(得分:0)
CONV(..., 10, 16)
将数字从基数10转换为16。LPAD(..., 16,'0')
UNHEX(...)
所以你的表达是UNHEX(LPAD(CONV(/* input value or column here */,10,16),16,'0'))
。
使用HEX()
函数将八位字节变回十六进制的示例,以便我们可以看到结果:
mysql> SET @input = 9; SET @output = UNHEX(LPAD(CONV(@input,10,16),16,'0')); SELECT HEX(@output);
+------------------+
| HEX(@output) |
+------------------+
| 0000000000000009 |
+------------------+
1 row in set (0.00 sec)
mysql> SET @input = 65536; SET @output = UNHEX(LPAD(CONV(@input,10,16),16,'0')); SELECT HEX(@output);
+------------------+
| HEX(@output) |
+------------------+
| 0000000000010000 |
+------------------+
1 row in set (0.00 sec)
-- largest possible bigint unsigned
mysql> SET @input = 18446744073709551615; SET @output = UNHEX(LPAD(CONV(@input,10,16),16,'0')); SELECT HEX(@output);
+------------------+
| HEX(@output) |
+------------------+
| FFFFFFFFFFFFFFFF |
+------------------+
1 row in set (0.00 sec)