SQL decimal and hexadecimal conversion

The CREATE  the FUNCTION ufn_ConvertInt2Hex ( @num  BIGINT )
 the RETURNS  VARCHAR ( 500 )
 the AS 
the BEGIN 
/ * ********************************* **** 
- function: decimal turn hex 
- author: GarsonZhang 
- time: May 28, 2016 13:26:55 
- test: 
the PRINT dbo.ufn_ConvertInt2Hex (50) 
***** ******************************** * / 

the DECLARE  @Result  VARCHAR ( 500 )
 the SET  @Result  =  '' 
the WHILE ( @ NUM  >  0 )
 BEGIN 
the SET  @Result =  The SUBSTRING ( ' 0123456789ABCDEF ' , @num  %  16  +  . 1 , . 1 )
 +  @Result 
the SET  @num  =  @num  /  16 
the END 
the RETURN  @Result 
the END 
the GO 

the CREATE  the FUNCTION ufn_ConvertHex2Int ( @HexString  VARCHAR ( 16 ))
 the RETURNS  BIGINT 
the AS  / * * ************************************ 
- function: hex decimal turn 
- author: GarsonZhang 
- time: May 28, 2016 13:30:24 
- test:
PRINT dbo.ufn_ConvertInt2Hex(50)
**************************************/
BEGIN 
DECLARE @Result BIGINT

DECLARE @i INT ,
@len INT

DECLARE @power BIGINT
SET @power = 16

SELECT @i = 0 ,
@Result = 0 ,
@HexString = RTRIM(LTRIM(UPPER(@HexString)))

SET @len = LEN(@HexString) 

IF( @Len  =  16 )
 the BEGIN  
the IF ( the ASCII ( the SUBSTRING ( @HexString , . 1 , . 1 )) >  55 )
 the BEGIN  
- to RaisError ( 'beyond the operation range data',. 1, 16) 
the RETURN  @Result 
the END  
the END  
- --- -------------------------------------------------- 
the WHILE ( @i  <  @len )
 the BEGIN  
the IF (( the 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

 

Guess you like

Origin www.cnblogs.com/laopo/p/11141718.html