THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Peter DeBetta's SQL Programming Blog

Peter DeBetta's blog about programming in SQL Server 2008, 2005, etc. using technologies such as T-SQL, .NET, CLR, C#, VB, Visual Studio, and SQL Server Management Studio.

T-SQL Convert Hex String to Varbinary

[EDIT] I made a few changes to account for the issues Chris and Cooter noted in the comments. 

One last post before I go on vacation next week...

SQL Server 2005 includes an undocumented function, sys.fn_varbintohexstr, that converts a hex value to a string representation of that hex value (0x3a becomes '0x3a'), but there is no function to go back from a hex string to a hex value. Now there is J:

CREATE FUNCTION dbo.HexStrToVarBin(@hexstr VARCHAR(8000)) 
RETURNS varbinary(8000
AS 
BEGIN 
   DECLARE 
@hex CHAR(2), @i INT@count INT@b varbinary(8000), @odd BIT@start bit
   
SET @count LEN(@hexstr
   
SET @start 1
   
SET @b CAST('' AS varbinary(1)) 
   
IF SUBSTRING(@hexstr12'0x' 
       
SET @i 
   
ELSE 
       SET 
@i 
   
SET @odd CAST(LEN(SUBSTRING(@hexstr@iLEN(@hexstr))) % AS BIT)
   
WHILE (@i <= @count
    
BEGIN 
       IF 
@start AND @odd 1
       
BEGIN
           SET 
@hex '0' SUBSTRING(@hexstr@i1)
       
END
       ELSE
       BEGIN
           SET 
@hex SUBSTRING(@hexstr@i2)
       
END
       SET 
@b @b 
               
CAST(CASE WHEN SUBSTRING(@hex11) LIKE '[0-9]' 
                   
THEN CAST(SUBSTRING(@hex11AS INT
                   
ELSE CAST(ASCII(UPPER(SUBSTRING(@hex11)))-55 AS INT
               
END 16 
               
CASE WHEN SUBSTRING(@hex21) LIKE '[0-9]' 
                   
THEN CAST(SUBSTRING(@hex21AS INT
                   
ELSE CAST(ASCII(UPPER(SUBSTRING(@hex21)))-55 AS INT
               
END AS binary(1)) 
       
SET @i @i + (- (CAST(@start AS INT) * CAST(@odd AS INT)))
       
IF @start 1
       
BEGIN
           SET 
@start 0
       
END
    END 
    RETURN 
@b 
END 
GO 


Published Friday, March 09, 2007 3:38 PM by Peter DeBetta
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Kedar Pathak said:

This is really usefull. It also works in MS SQL 2000

April 9, 2007 5:39 AM
 

Technologies & Programming » Blog Archive » bla2 said:

April 20, 2007 6:05 PM
 

Microsoft » Blog Archives » … and decouple your Transact-SQL (T-SQL) code well, said:

June 13, 2007 6:53 PM
 

wally said:

'15E' returns 5577 instead of 350

October 31, 2007 9:12 AM
 

MichaelDotNet said:

It's important to note that it requires an even number of hexits, so for instance '015E' DOES return 350

November 13, 2007 12:03 PM
 

MichaelDotNet said:

I posted an updated version on my blog, it handles odd numbers of hex digits (and stops at the first non hex character rather then looking for "0x").  It uses bigints though so is somewhat limited: http://michaeldotnet.blogspot.com/2007/11/t-sql-hex-string-to-varbinary-improved.html

November 13, 2007 3:20 PM
 

Rexiology@MSDN said:

Here is some conversion functions in SQL Server... To convert between int and hex values, by referring

January 12, 2008 3:29 PM
 

Rexiology::Work said:

crosspost from http://blogs.msdn.com/rextang Here is some conversion functions in SQL Server... To convert

January 12, 2008 3:30 PM
 

Noticias externas said:

Here is some conversion functions in SQL Server... To convert between int and hex values, by referring

January 12, 2008 3:44 PM
 

MSDN Blog Postings » T-SQL: Convert Hex String to VarBinary... said:

January 12, 2008 6:14 PM
 

The Prokrammer » Blog Archive » T-SQL Hex String to VarBinary (Improved) said:

March 4, 2008 8:30 PM
 

Chris said:

it works much better i "SET @b = @b +..." is more correctly "SET @b = @b * 256 +..."  This is needed to left shift the value of b clearing out the lower byte for the next conversion.   Otherwise this only works for a byte as in 0x12, but would not work for 0x12345678

Chris

August 15, 2008 3:06 PM
 

Cooter said:

Riddle me this...

declare @tmp varchar(8)

set @tmp = '0x514'

select dbo.HexStrToVarBin(@tmp),sys.fn_varbintohexstr(dbo.HexStrToVarBin(@tmp))

input doesnt match output ... am I missing the obvious ... ?

August 26, 2008 10:44 PM
 

Peter DeBetta said:

I changed the post to include the code with the fix for the issues both Chris and Coote noted.

August 27, 2008 3:26 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Peter DeBetta

Peter DeBetta is an independent consultant specializing in design, development, implementation, and deployment of Microsoft SQL Server, Microsoft SharePoint Server, and .NET solutions. Peter writes courseware, articles, and books – most notably the title Introducing SQL Server 2005 for Developers from Microsoft Press. Peter speaks at conferences around the world, including TechEd, SQL PASS Community Summit, DevTeach, SQL Connections, DevWeek, and VSLive!

Peter is a Microsoft MVP for SQL Server, an MCP, President of the North Texas SQL Server User Group, and a member of PASS.

When Peter isn’t working, you can find him singing and playing guitar (click here to hear an original song by Peter), taking pictures, or simply enjoying life with his wife, son, and new daughter.
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement