Friday, March 9, 2012

Decimal to Hex formatting

I have a specific SQL Server coding problem.
I have an 8 character field that is an integer.
eg: 16776976
In hex this is FFFF10
This represents the Red, Green and Blue hex values for a colour, in
reverse order:
ie. 10 is red, FF is green, FF is blue
I need my SQL Server stored procedure to be able to get from the 8
character field to the red, green and blue codes. How do I get 16776976
into FFFF10 so I can then simply substring the appropriate values?
MarkHi
SELECT CAST(16776976 AS VARBINARY(10))
"stainless" <Mark.Wingfield@.gmail.com> wrote in message
news:1123583639.609068.92820@.g49g2000cwa.googlegroups.com...
>I have a specific SQL Server coding problem.
> I have an 8 character field that is an integer.
> eg: 16776976
> In hex this is FFFF10
> This represents the Red, Green and Blue hex values for a colour, in
> reverse order:
> ie. 10 is red, FF is green, FF is blue
> I need my SQL Server stored procedure to be able to get from the 8
> character field to the red, green and blue codes. How do I get 16776976
> into FFFF10 so I can then simply substring the appropriate values?
> Mark
>|||This will return 16776976 as 0x00FFFF10 in a varbinary format. However,
as this is not a text field, it is not a simple substring command to
get FF, FF and 10 out of this string. How can I extract these bytes
from this varbinary field so I actually have FF, FF and 10 as actual
text?
(I may also have to convert these into decimal integers but that is in
another step e.g. FF is 255)
Cheers
Mark|||Mark, perhaps this can help:
CREATE FUNCTION dbo.fn_dectobase(@.val AS bigint, @.base AS int)
RETURNS varchar(63)
AS
BEGIN
DECLARE @.r AS varchar(63), @.alldigits AS varchar(36);
SET @.alldigits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET @.r = '';
WHILE @.val > 0
BEGIN
SET @.r = SUBSTRING(@.alldigits, @.val % @.base + 1, 1) + @.r;
SET @.val = @.val / @.base;
END
RETURN @.r;
END
GO
SELECT dbo.fn_dectobase(16776976, 16);
Output: FFFF10
BG, SQL Server MVP
www.SolidQualityLearning.com
"stainless" <Mark.Wingfield@.gmail.com> wrote in message
news:1123583639.609068.92820@.g49g2000cwa.googlegroups.com...
>I have a specific SQL Server coding problem.
> I have an 8 character field that is an integer.
> eg: 16776976
> In hex this is FFFF10
> This represents the Red, Green and Blue hex values for a colour, in
> reverse order:
> ie. 10 is red, FF is green, FF is blue
> I need my SQL Server stored procedure to be able to get from the 8
> character field to the red, green and blue codes. How do I get 16776976
> into FFFF10 so I can then simply substring the appropriate values?
> Mark
>|||On 9 Aug 2005 03:33:59 -0700, stainless wrote:

>I have a specific SQL Server coding problem.
>I have an 8 character field that is an integer.
>eg: 16776976
>In hex this is FFFF10
>This represents the Red, Green and Blue hex values for a colour, in
>reverse order:
>ie. 10 is red, FF is green, FF is blue
>I need my SQL Server stored procedure to be able to get from the 8
>character field to the red, green and blue codes. How do I get 16776976
>into FFFF10 so I can then simply substring the appropriate values?
Hi Mark,
The following will give you the integer values for R, G and B:
declare @.num int
set @.num = 16776976
select @.num / 65536 AS R, @.num % 65536 / 256 AS G, @.num % 256 AS B
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment