在MySQL中将二进制(16)列显示为十六进制 [英] Display binary(16) column as hex in mysql
问题描述
给出一个二进制(16)列,如何将其值显示为十六进制数?我一直在下面的控制台中进行实验,但未获得预期的结果.可能是我没有将数字正确转换为二进制吗?
Given a binary(16) column, how do I display its value as a hexadecimal number? I've been experimenting in the console a little below, and I'm not getting the results I expect. Could it be that I'm not converting numbers to binary properly?
我有以下示例查询:select hex(cast(10 as binary))
,
,但输出结果出乎意料:3130
.
but the output is rather unexpected: 3130
.
基本上,它看起来只是采用数字的十进制表示,并在每个数字之前插入一个3
数字,例如2
-> 32
,22
-> 3232
,678
-> 363738
等
Basically, it appears to just take the decimal representation of the number, and insert a 3
digit before each digit, e.g. 2
-> 32
, 22
-> 3232
, 678
-> 363738
, etc.
如果我为二进制数据类型指定了一个长度(例如binary(16)
),除了会在右侧填充所需数量的0
s之外,我会得到相同的行为.
If I specify a length for the binary data type (e.g. binary(16)
), I get the same behavior, except that it gets padded on the right with the required number of 0
s.
显然,这不是我想要的.我想念什么?
Obviously, that's not what I'm looking for. What am I missing?
只是尝试了convert(678, binary)
而不是cast
,所以行为相同.
just tried convert(678, binary)
instead of cast
, same behavior.
推荐答案
为什么使事情变得复杂?我只是用它来显示BINARY(16)
列的值:
Why make things complicated? I just use this to display the value of a BINARY(16)
column:
SELECT HEX(colname);
这篇关于在MySQL中将二进制(16)列显示为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!