Oracle : Converting Hex Strings to Binary

Created 22 September 2004 00:00
CREATE PROCEDURE ConvertHexStrToBinary 
  @hexstr varchar(1000),
  @binValue binary(8) OUTPUT
AS
  DECLARE @index tinyint,
          @result bigint,
          @digit int,
          @convstr varchar(15)
	
  SELECT  @index = LEN(@hexstr),
          @result = 0,
          @convstr = '123456789ABCDEF'
	
  WHILE (@index > 2)
  BEGIN
    SELECT @digit = CHARINDEX(SUBSTRING(@hexstr,@index,1),@convstr)
    SELECT @result = @result + @digit * POWER(CONVERT(numeric, 16), LEN(@hexstr) - @index)
    SELECT @index = @index - 1
  END

  SELECT @binValue = CONVERT(binary(8),@result)
GO