HexToChar

USE AdventureWorks2008R2;

GO

IF EXISTS(SELECT name FROM sys.objects

          WHERE name = N'dbo.sp_HexToChar')

    DROP PROCEDURE HexToChar;

GO

CREATE PROCEDURE dbo.sp_HexToChar

    @BinValue varbinary(255),

    @HexCharValue nvarchar(255) OUTPUT

AS

    DECLARE @CharValue nvarchar(255);

    DECLARE @Position int;

    DECLARE @Length int;

    DECLARE @HexString nchar(16);

    SELECT @CharValue = N'0x';

    SELECT @Position = 1;

    SELECT @Length = DATALENGTH(@BinValue);

    SELECT @HexString = N'0123456789ABCDEF';

    WHILE (@Position <= @Length)

    BEGIN

        DECLARE @TempInt int;

        DECLARE @FirstInt int;

        DECLARE @SecondInt int;

        SELECT @TempInt = CONVERT(int, SUBSTRING(@BinValue,@Position,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 @Position = @Position + 1;

    END

    SELECT @HexCharValue = @CharValue;

GO

DECLARE @BinVariable varbinary(35);

DECLARE @CharValue nvarchar(35);



SET @BinVariable = 123456;



EXECUTE dbo.sp_HexToChar

    @binvalue = @BinVariable,

    @HexCharValue = @CharValue OUTPUT;



SELECT @BinVariable AS BinaryValue,

    @CharValue AS CharacterRep;

GO

你可能感兴趣的:(char)