UNKNOWN --************************************** -- Name: Hex to Binary Converter -- Description:Converts Hexadecimal to Binary -- By: Shivanand Iyer -- -- -- Inputs:Hex input as varchar -- -- Returns:Binary Digits as varchar -- --Assumes:None -- --Side Effects:None --This code is copyrighted and has limited warranties. --Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.1046/lngWId.5/qx/vb/scripts/ShowCode.htm --for details. --************************************** CREATE FUNCTION FN_HEXTOBINARY (@HEX VARCHAR(8)) RETURNS VARCHAR(255) AS BEGIN DECLARE @BASE TINYINT DECLARE @string VARCHAR(255) DECLARE @return VARCHAR(255) DECLARE @div INT DECLARE @char CHAR(1) DECLARE @pos INT DECLARE @digit INT DECLARE @result varchar(16) SELECT @string= '0123456789ABCDEF' SELECT @pos = 1 SELECT @result= '' SELECT @return= '' select @base=2 while (@pos<>len(@hex)+1) begin if substring(@hex,@pos,1) not in ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F') begin return NULL end select @digit=charindex(substring(@hex,@pos,1),@string)-1 WHILE (@digit<>0) BEGIN select @div=@digit/@base select @result=convert(varchar(1),(@digit % @base)) + @result select @digit=@div END select @return=@return + case when len(@result)<4 then replace(space(4-len(@result)) + @result,' ','0') else @result end select @result='' select @pos=@pos+1 end RETURN @return END