Convert Numeric To Hex

,

a better way to convert numeric value to Hex string

CREATE PROCEDURE  sp_GetHexFromNumeric 
@StringID nvarchar(8) output,
@ID numeric

 AS

declare @HEX_STRING nchar(20)
declare @HEX_CHAR   char(1)
declare @Pos int
declare @TempID numeric
declare @Divisor int 

set @HEX_STRING =  '0123456789ABCDEF'
set @StringID = ''
set @TempID = @ID
---set @Divisor = 4096  --Support in 8 char
set @Divisor = 268435456

---if @ID > 65536
if @ID > 4294967296
set @StringID = 'FFFFFFFF'
else
    BEGIN

    while (@Divisor>0)
    BEGIN

set @Pos = @TempID/@Divisor
             if(@Pos>15)
               set @Pos =15
set @HEX_CHAR = substring(@HEX_STRING,@Pos+1,1)

set @StringID = @StringID+@HEX_CHAR
set @TempID = @TempID - @Pos*@Divisor

set @Divisor  = @Divisor/16

    END

END
GO

Rate

Share

Share

Rate