gpt4 book ai didi

MySQL 十六进制字符串低字节在前

转载 作者:行者123 更新时间:2023-11-29 11:19:24 26 4
gpt4 key购买 nike

如果我将 0xFF 插入二进制列,MySql (5.7) 会假定这些是高字节。

例如如果列是 BINARY(2):

+--------------------+
| HEX(binary_column) |
+--------------------+
| FF00 |
+--------------------+

为了方便起见,如何让 MySql 正常解释十六进制字符串?

附注还尝试过 UNHEX()

最佳答案

binary 并不是真正的数字数据类型。它是一种特殊类型的字符串,用于存储文件等二进制数据。与例如相反charbinary 没有字符映射表,并且通过数字代码进行比较。

该行为与其他编程语言处理字符串和字节数组的方式类似,并且在 mysql 中也是如此,请参阅 The BINARY and VARBINARY Type :

When BINARY values are stored, they are right-padded with the pad value to the specified length. The pad value is 0x00 (the zero byte). Values are right-padded with 0x00 on insert, and no trailing bytes are removed on select. All bytes are significant in comparisons, including ORDER BY and DISTINCT operations. 0x00 bytes and spaces are different in comparisons, with 0x00 < space.

您似乎在寻找二进制数字,因此您可能想要使用数字类型。您可以使用例如int (或 bit(16)),并且仍然插入诸如 0xFF 之类的值(只是不作为 '0xFF' 没有进一步类型转换),并且您仍然可以使用例如显示它们hex(0xFF) 按照您想要的方式。

如果您想使用二进制值(或需要> 8字节的大值),您可以使用 lpad用前导零填充它们,例如

select hex(lpad(0xFF,2,0x0))

您必须知道(或查询)列的大小,并且从添加两个二进制值的简单任务开始,您可能会遇到很多问题。因此,为了简单起见,请使用数字类型。

关于MySQL 十六进制字符串低字节在前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com