CREATE PROCEDURE sp_hexadecimal
    @binvalue varbinary(255),
    @hexvalue varchar(255) OUTPUT
AS
DECLARE @charvalue varchar(255), @i int, @length int, @hexstring char(16),
        @tempint int, @firstint int, @secondint int
SELECT @charvalue = '0x', @i = 1, @length = DATALENGTH(@binvalue),
        @hexstring = '0123456789abcdef'
WHILE (@i <= @length)
BEGIN
    SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
    SELECT @firstint = FLOOR(@tempint/16)
    SELECT @secondint = @tempint - (@firstint*16)
    SELECT @charvalue = @charvalue +
        SUBSTRING(@hexstring, @firstint+1, 1) +
        SUBSTRING(@hexstring, @secondint+1, 1)
    SELECT @i = @i + 1
END
SELECT @hexvalue = @charvalue
Go
This page was last updated on May 01, 2006 04:28 PM.