MySQL 插入十六进制值mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4306219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:51:29  来源:igfitidea点击:

Inserting hex value mysql

mysqldata-storage

提问by user524156

I have created an sql database using Java, I am a noob when it comes to sql but have it configured and set up, i have a table created which has two columns, the first being a big integer which increments, the second I have tried defining it as a char, varchar and binary but im still not getting the desired function, say i try and store 0a a hex number into the char column i get an error, i appended 0x to the beginning a it seems to store, but when i print out the contents it is blank or in some cases i get characters such as '/' or '?', I also tried using sql explorer and it gives me the same result viewing the table,

我已经使用 Java 创建了一个 sql 数据库,我在 sql 方面是个菜鸟,但是配置和设置了它,我创建了一个包含两列的表,第一个是一个递增的大整数,第二个我尝试过将其定义为 char、varchar 和 binary 但我仍然没有得到所需的函数,比如说我尝试将 0a 一个十六进制数存储到 char 列中我得到一个错误,我将 0x 附加到它似乎存储的开头,但是当我打印出来的内容是空白的,或者在某些情况下我得到诸如“/”或“?”之类的字符,我也尝试使用 sql explorer 并且它给了我相同的结果查看表,

My problem is i need to store an eight character hex string such as eb8d4ee6.

我的问题是我需要存储一个 8 个字符的十六进制字符串,例如 eb8d4ee6。

Could someone please advise me of how this can be done?

有人可以告诉我如何做到这一点吗?

回答by Riedsio

Here's a great blog post I always refer to to remind myself of the proper handling of hex values and binary fields, and lays out some performance implications.

这是一篇很棒的博客文章,我总是参考以提醒自己正确处理十六进制值和二进制字段,并列出了一些性能影响。

http://www.xaprb.com/blog/2009/02/12/5-ways-to-make-hexadecimal-identifiers-perform-better-on-mysql/

http://www.xaprb.com/blog/2009/02/12/5-ways-to-make-hexadecimal-identifiers-perform-b​​etter-on-mysql/

回答by RedFilter

See http://dev.mysql.com/doc/refman/5.5/en/hexadecimal-literals.html

http://dev.mysql.com/doc/refman/5.5/en/hexadecimal-literals.html

MySQL supports hexadecimal values, written using X'val', x'val', or 0xval format, where val contains hexadecimal digits (0..9, A..F). Lettercase of the digits does not matter. For values written using X'val' or x'val' format, val must contain an even number of digits. For values written using 0xval syntax, values that contain an odd number of digits are treated as having an extra leading 0. For example, 0x0a and 0xaaa are interpreted as 0x0a and 0x0aaa.

In numeric contexts, hexadecimal values act like integers (64-bit precision). In string contexts, they act like binary strings, where each pair of hex digits is converted to a character:

MySQL 支持十六进制值,使用 X'val'、x'val' 或 0xval 格式编写,其中 val 包含十六进制数字 (0..9, A..F)。数字的字母大小写无关紧要。对于使用 X'val' 或 x'val' 格式写入的值,val 必须包含偶数位数。对于使用 0xval 语法编写的值,包含奇数位数的值被视为具有额外的前导 0。例如,0x0a 和 0xaaa 被解释为 0x0a 和 0x0aaa。

在数字上下文中,十六进制值的作用类似于整数(64 位精度)。在字符串上下文中,它们的作用类似于二进制字符串,其中每对十六进制数字都转换为一个字符:

You probably should store the Hex number in an integer column. You can then convert back to hex when selecting using the HEX()function.

您可能应该将十六进制数存储在整数列中。然后,您可以在选择使用该HEX()功能时转换回十六进制。

E.g.,

例如,

INSERT INTO MyTable (`MyIntegerColumn`) VALUES (0xeb8d4ee6);