Technical Article

Optimized HexToSMALLINT

,

An optimized function for converting a HEX string into a SMALLINT. Built for being faster then other HexToDec converters.
If there is any faster function to do this conversion, I am intrested of using it instead.

/*
Input has to be a Valid Hexadecimal number. Example 4F or 04F or 004F. 
7FFF is the maximum value and it gives 32767 (Max SMALLINT)

Hans Lindgren
*/CREATE FUNCTION dbo.HexToSMALLINT 
(
@Value VARCHAR(4)
)
RETURNS SMALLINT
AS
BEGIN

SET @Value =  REVERSE( RIGHT( UPPER( '0000' + @Value ) , 4 ) )

RETURN (
                (CHARINDEX( SUBSTRING( @Value , 1 , 1 ) , '0123456789ABCDEF' , 1 ) - 1 ) +
      16*       (CHARINDEX( SUBSTRING( @Value , 2 , 1 ) , '0123456789ABCDEF' , 1 ) - 1 ) +
      256*      (CHARINDEX( SUBSTRING( @Value , 3 , 1 ) , '0123456789ABCDEF' , 1 ) - 1 ) +
      4096*     (CHARINDEX( SUBSTRING( @Value , 4 , 1 ) , '0123456789ABCDEF' , 1 ) - 1 )
)
END
GO


SELECT 
dbo.HexToSMALLINT('0ABC') , 
dbo.HexToSMALLINT('7FFF') , 
dbo.HexToSMALLINT('0FFF') , 
dbo.HexToSMALLINT('0')    AS MinValue, 
dbo.HexToSMALLINT('7FFF') AS MaxValue

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating