SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

HexToSmallInt

By Aaron West, 2005/05/16

Total article views: 30 | Views in the last 30 days: 3

Hans asked if it could be faster. This is about 10% faster; not much. His is admittedly more readable, and mine will act very strangely with invalid hex digits.

How does it work? I'm converting the string '1234' to the value 0x31323334 (for example), then subtracting '0000' so that it is 0-based in each byte (CONVERT(INT,0x30303030) = 808464432), then masking out each byte, subtracting 7 if the value is between 16 and 31 instead of 0-9 (since the hexadecimal digits will always have that bit set), then shifting to the appropriate place with division, and adding the nybbles together.

Confused? Me too. But it works. (So use Hans's; it's easier to understand. Not sure a 10% to 20% speed boost is worth this.)

By Aaron West, 2005/05/16

Total article views: 30 | Views in the last 30 days: 3
Your response
 
 
Related tags

Miscellaneous    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com