CREATE FUNCTIONUfn_convertint2hex (@Num BIGINT )RETURNS VARCHAR( -) asBEGIN/**************************************--function: Decimal to hex--garsonzhang--time: May 28, 2016 13:26:55--test: PRINT dbo.ufn_ Convertint2hex (*************************************)*/DECLARE @Result VARCHAR( -)SET @Result = "' while(@Num > 0 )BEGINSET @Result = SUBSTRING('0123456789ABCDEF',@Num % - + 1,1)+ @ResultSET @Num = @Num / -ENDRETURN @ResultENDGOCREATE FUNCTIONUfn_converthex2int (@HexString VARCHAR( -) )RETURNS BIGINT as /**************************************--function: Hexadecimal to decimal--garsonzhang--time: May 28, 2016 13:30:24--test: PRINT dbo.ufn_ Convertint2hex (*************************************)*/BEGIN DECLARE @Result BIGINTDECLARE @i INT ,@len INTDECLARE @power BIGINTSET @power = -SELECT @i = 0 ,@Result = 0 ,@HexString = RTRIM(LTRIM(UPPER(@HexString)))SET @len = LEN(@HexString) IF(@len = - )BEGIN IF(ASCII(SUBSTRING(@HexString,1,1))> - )BEGIN --RaisError (' Out of data range ', 1, +)RETURN @ResultEND END ------------------------------------------------------- while(@i < @len )BEGIN IF( (SUBSTRING(@HexString,@len - @i,1) not between '0' and '9' ) and(SUBSTRING(@HexString,@len - @i,1) not between 'A' and 'F' ))BEGIN SET @Result = 0 Break;END ---------------------------------------- SET @Result = @Result +(CHARINDEX(SUBSTRING(@HexString,@len - @i,1),'0123456789ABCDEF')- 1 )* CAST(POWER(@power,@i) as BIGINT)SET @i = @i + 1 END ---------------------------------------------- RETURN @Result END
SQL decimal and hexadecimal conversions