How do you convert an integer into a string of hex? I want to convert the int into a format that I can use as a color on my page for example '#ff0000'.
如何将整数转换为十六进制的字符串?我想将int转换为我可以在页面上用作颜色的格式,例如'#ff0000'。
So for example:
例如:
--This converts my int to hex:
CONVERT(VARBINARY(8), Color) Color,
And I want to do something like this:
我想做这样的事情:
'#' + CONVERT(NVARCHAR(10), CONVERT(VARBINARY(8), Color)) Color
But converting a varbinary string just converts it to an ascii character rather than returning the actual hex string
但转换varbinary字符串只是将其转换为ascii字符,而不是返回实际的十六进制字符串
2 个解决方案
#1
7
There is a built in function to generate hex strings from binary values
有一个内置函数可以从二进制值生成十六进制字符串
SELECT
'#' + sys.fn_varbintohexstr(CONVERT(BINARY(3), 0)),
'#' + sys.fn_varbintohexstr(CONVERT(BINARY(3), 255))
You need binary(3)
to ensure the correct length of the output string
This is wrong. You get 4 hex digits because 0 and 255 here are 4 byte int
values
你需要二进制(3)来确保输出字符串的正确长度这是错误的。你得到4个十六进制数字,因为0和255这里是4个字节的int值
SELECT
'#' + sys.fn_varbintohexstr(CONVERT(varBINARY(8), 0)),
'#' + sys.fn_varbintohexstr(CONVERT(varBINARY(8), 255))
Oct 2017 Update:
2017年10月更新:
The conversion is now built-in to SQL Server (since 2008!!) so we can simply use CONVERT
现在转换内置于SQL Server(自2008年起!!),因此我们可以简单地使用CONVERT
SELECT '#' + CONVERT(char(6), CONVERT(BINARY(3), 2570841), 2)
#2
2
Conversion from int to varbinary is implicit in SQL SERVER, so you can also use SELECT sys.fn_varbintohexstr(1234567) and you're done.
在SQL SERVER中隐含从int到varbinary的转换,因此您也可以使用SELECT sys.fn_varbintohexstr(1234567)并完成。
But beware BIGINT values, because long numeric literals are interpreted as DECIMAL values and not as BIGINT. Decimals have prefix data to hold the precision, and the byte order is reversed, and that's why you get this:
但要注意BIGINT值,因为长数字文字被解释为DECIMAL值而不是BIGINT。小数有前缀数据来保持精度,字节顺序是相反的,这就是你得到这个的原因:
select sys.fn_varbintohexstr(2147483648)
returns 0x0A00000100000080
返回0x0A00000100000080
You need to convert explicitly to BIGINT:
您需要显式转换为BIGINT:
select select sys.fn_varbintohexstr(CONVERT(BIGINT(2147483648))
returns 0x0000000080000000
返回0x0000000080000000
#1
7
There is a built in function to generate hex strings from binary values
有一个内置函数可以从二进制值生成十六进制字符串
SELECT
'#' + sys.fn_varbintohexstr(CONVERT(BINARY(3), 0)),
'#' + sys.fn_varbintohexstr(CONVERT(BINARY(3), 255))
You need binary(3)
to ensure the correct length of the output string
This is wrong. You get 4 hex digits because 0 and 255 here are 4 byte int
values
你需要二进制(3)来确保输出字符串的正确长度这是错误的。你得到4个十六进制数字,因为0和255这里是4个字节的int值
SELECT
'#' + sys.fn_varbintohexstr(CONVERT(varBINARY(8), 0)),
'#' + sys.fn_varbintohexstr(CONVERT(varBINARY(8), 255))
Oct 2017 Update:
2017年10月更新:
The conversion is now built-in to SQL Server (since 2008!!) so we can simply use CONVERT
现在转换内置于SQL Server(自2008年起!!),因此我们可以简单地使用CONVERT
SELECT '#' + CONVERT(char(6), CONVERT(BINARY(3), 2570841), 2)
#2
2
Conversion from int to varbinary is implicit in SQL SERVER, so you can also use SELECT sys.fn_varbintohexstr(1234567) and you're done.
在SQL SERVER中隐含从int到varbinary的转换,因此您也可以使用SELECT sys.fn_varbintohexstr(1234567)并完成。
But beware BIGINT values, because long numeric literals are interpreted as DECIMAL values and not as BIGINT. Decimals have prefix data to hold the precision, and the byte order is reversed, and that's why you get this:
但要注意BIGINT值,因为长数字文字被解释为DECIMAL值而不是BIGINT。小数有前缀数据来保持精度,字节顺序是相反的,这就是你得到这个的原因:
select sys.fn_varbintohexstr(2147483648)
returns 0x0A00000100000080
返回0x0A00000100000080
You need to convert explicitly to BIGINT:
您需要显式转换为BIGINT:
select select sys.fn_varbintohexstr(CONVERT(BIGINT(2147483648))
returns 0x0000000080000000
返回0x0000000080000000