在 MySQL 中将十六进制转换为二进制
目前我在MySQL中搜索一个函数来进行十六进制字符串到二进制表示之间的转换,例如:
0000 -> 0000000000000000
00AA -> 0000000010101010
FFFF -> 1111111111111111
我已经尝试过
UNHEX('00AA')
CAST('00AA' AS BINARY)
CONVERT('00AA', BINARY)
但没有得到我想要的结果。
Currently I search a function in MySQL to do conversion between hex string to binary representation, example:
0000 -> 0000000000000000
00AA -> 0000000010101010
FFFF -> 1111111111111111
I have already tried
UNHEX('00AA')
CAST('00AA' AS BINARY)
CONVERT('00AA', BINARY)
but didn't get the results I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
CONV()
函数:根据输入获得长度:
由于
CONV()
使用 64 位精度,因此转换的位数不能超过 64 位,因此您也可以使用它:并且您应该检查
LENGTH(string) <= 16
否则您可能会得到错误的结果。Use
CONV()
function:To have length according to input:
As
CONV()
works with 64-bit precision, you can't have more than 64 bits converted, so you can use this as well:and you should check that
LENGTH(string) <= 16
or you may get erroneous results.将传递给函数的字符串中的每一对字符解释为两个十六进制字符,并尝试将其转换为二进制数(mysql 中的二进制格式)。这样,您就可以将字符串转换为二进制。但是,这不会将内容显示为二进制数字字符串。相反,结果字符串中的每 2 个字节都会转换为特定的字符编码(例如 utf8)。
Would interpret each pair of characters in a string passed into the function as two hex characters and try to convert that to a binary number (binary format in mysql). This way, you can convert a string to binary. However, this won't display the content as a string of binary digits. Rather, each 2 bytes in the resulting string converted to the specific character encoding (for example utf8).