MySQL 如何将字符串转换为十六进制,反之亦然?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8339138/
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 21:53:12  来源:igfitidea点击:

How to convert a String to Hex and vice-versa?

mysqlsql-server

提问by Freeman

As you know , in MySQL we have HEX & UNHEX, for example when I write like this :

如您所知,在 MySQL 中我们有 HEX 和 UNHEX,例如当我这样写时:

select hex("Ali");

the convert result is : 416C69

转换结果为:416C69

and the unhex is like this : select unhex("416C69");

和 unhex 是这样的: select unhex("416C69");

In MSSQL I can't convert this, could you please give me an example for both of them ???

在 MSSQL 中我无法转换这个,你能给我举个例子吗???

Thanks a lot ...

非常感谢 ...

回答by Zohaib

I think this should work

我认为这应该有效

SELECT     hex(CAST("Ali" AS VARBINARY)) AS Expr1

for vice versa

反之亦然

select CONVERT(varbinary(max), "416C69");

This will convert to varbinary, then you can convert varbinary to varchar

这将转换为 varbinary,然后您可以将 varbinary 转换为 varchar

Converting a String to HEX in SQL

在 SQL 中将字符串转换为 HEX

http://blogs.msdn.com/b/sqltips/archive/2008/07/02/converting-from-hex-string-to-varbinary-and-vice-versa.aspx

http://blogs.msdn.com/b/sqltips/archive/2008/07/02/converting-from-hex-string-to-varbinary-and-vice-versa.aspx