INF: Converting Binary Data to Hexadecimal String

Last reviewed: April 28, 1997
Article ID: Q104829
The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2
  - Microsoft SQL Server version 4.2

SUMMARY

SQL Server does not have built-in Transact-SQL command for converting binary data to a hexadecimal string.

MORE INFORMATION

The Transact-SQL CONVERT command converts binary data to character data in a one byte to one character fashion. SQL Server takes each byte of the source binary data, converts it to an integer value, then uses that integer value as the ASCII value for the destination character data. This behavior applies to the binary, varbinary, and timestamp datatypes.

For example, binary value 00001111 (0x0F in hexadecimal) is converted into its integer equivalent which is 15, then converted to the character that corresponds to ASCII value 15, which is unreadable.

The following stored procedure can be used to return a character string which contains the hexadecimal representation of a binary value:

   create procedure sp_hexadecimal
     @binvalue varbinary(255)
   as
   declare @charvalue varchar(255)
   declare @i int
   declare @length int
   declare @hexstring char(16)

   select @charvalue = '0x'
   select @i = 1
   select @length = datalength(@binvalue)
   select @hexstring = "0123456789abcdef"

   while (@i <= @length)
   begin

     declare @tempint int
     declare @firstint int
     declare @secondint int

     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 'sp_hexadecimal'=@charvalue

For example, when the following command batch is executed with ISQL:

     declare @bin varbinary(255)
     select @bin = @@dbts
     execute sp_hexadecimal @bin
     select 'isql' = @bin
     go

it returns output similar to the following:

     sp_hexadecimal
     ------------------------------------------------------------------
     0x01000000a60b0000

     (1 row affected)

     isql
     ------------------------------------------------------------------
     0x01000000a60b0000
     (1 row affected)


Additional query words: Windows NT
Keywords : kbother SSrvServer SSrvWinNT
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS
Resolution Type : kbcode


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.