在SQL Server 2012
中,您如何将由int
(存储为0xBBGGRR)表示的颜色转换为格式为R=123, G=234, B=12
的字符串?我们将忽略alpha部分。
E.g。给定值0xff223344
的整数,您将获得R=68, G=51, B=34
。
答案 0 :(得分:2)
使用按位和(&)运算符:
declare @i int
set @i=0xff223344
select
'R=' + cast(@i & 0xFF as varchar(3)) +
', G=' + cast((@i & 0xFF00) / 0x100 as varchar(3)) +
', B=' + cast((@i & 0xFF0000) / 0x10000 as varchar(3))
答案 1 :(得分:1)
Declare @i int
Set @i=0xff223344
select
'R=' + Cast(Cast(SubString(CAST(@i as BINARY(4)),4,1) as int) as Varchar(3))
+ ', G=' + Cast(Cast(SubString(CAST(@i as BINARY(4)),3,1) as int) as Varchar(3))
+ ', B=' + Cast(Cast(SubString(CAST(@i as BINARY(4)),2,1) as int) as Varchar(3))
答案 2 :(得分:1)
这样的解决方案怎么样?
declare @i int = 0x223344
select 'R=' + cast (@i % 256 as varchar)
+ ', G=' + cast (@i / 256 % 256 as varchar)
+ ', B=' + cast (@i / 65536 % 256 as varchar)
或者如果你有alpha通道的ff
:
declare @i int = 0xff443322
if @i < 0
set @i = @i % cast(0x1000000 as int) - cast(0xff000000 as int)
select 'R=' + cast (@i % 256 as varchar)
+ ', G=' + cast (@i / 256 % 256 as varchar)
+ ', B=' + cast (@i / 65536 % 256 as varchar)