MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 3rd, 2008, 04:20 PM
WileBean WileBean is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 3 WileBean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 36 m 54 sec
Reputation Power: 0
Converting hex varchar to decimal bigint

Hi there,

I'm trying to convert the varchar MAC address '0000123ff2a5' (hex) into it's decimal representation as a bigint.

I've tried several T-SQL functions but none of them have successfully converted the value. The result should be '0000306180773158'. So, what I'm trying to do is convert a MAC address (hex) into the equipment address (decimal).

Any ideas?

Thanks so much,
Kristina

Reply With Quote
  #2  
Old July 9th, 2008, 09:39 AM
pdreyer pdreyer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 45 pdreyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 54 m 17 sec
Reputation Power: 1
Code:
create function hex2int(@s varchar(16)) --Convert hex to 
returns bigint -- e.g. select dbo.hex2int('7ff2a5')
as begin
select @s=upper(@s)
declare @i int, @len int, @c char(1), @result bigint
select @len=len(@s), @i=@len, @result=case when @len>0 then 0 end
while (@i>0)
begin
  select @c=substring(@s,@i,1), @result=@result
  +(ASCII(@c)
    -(case when @c between 'A' and 'F' then 55 
      else case when @c between '0' and '9' then 48 end
      end))
  *power(16.,@len-@i)
  ,@i=@i-1
end -- while
return @result
end -- function

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Converting hex varchar to decimal bigint


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT