将十六进制值插入 MySql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3126210/
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
Insert hex values into MySql
提问by Tim
I have a table with a VARBINARY column. I need to insert a string such as '4D2AFF' which represents the hex values 0x4D, 0x2A and 0xFF respectively. How do I construct this statement?
我有一个带有 VARBINARY 列的表。我需要插入一个字符串,例如 '4D2AFF',它分别代表十六进制值 0x4D、0x2A 和 0xFF。我如何构造这个语句?
回答by BalusC
回答by nawfal
回答by aronchick
The Hex() and Unhex() functions were already mentioned, but I'd like to weigh in as well on an alternative pattern.
已经提到了 Hex() 和 Unhex() 函数,但我还想权衡另一种模式。
How are you using the strings before and after? If you can avoid coversion until the last possible minute, that is highly preferential. That way you don't risk something going wrong, or forgetting whether or not the object you've extracted from the DB has already been converted or not.
你是如何使用之前和之后的字符串的?如果你能在最后一分钟之前避免覆盖,那是非常优先的。这样你就不会冒出错的风险,也不会忘记你从数据库中提取的对象是否已经被转换。
回答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/