AutoHotkey Community

It is currently May 27th, 2012, 2:14 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: February 3rd, 2010, 10:46 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
I decided to create a couple functions for converting decimal numbers to binary and vica-versa.

Here they are:
Code:
Bin(x){
   while x
      r:=1&x r,x>>=1
   return r
}
Dec(x){
   b:=StrLen(x),r:=0
   loop,parse,x
      r|=A_LoopField<<--b
   return r
}

I tried several different versions, and these are the fastest (and shortest) I could come up with.

If you're interested, I first created some one-liners that do the same thing. Though I must warn that they are both slower and longer: (longer in the number of non-whitespace characters)
Code:
Bin(x){
   return (x>>1 ? Bin(x>>1):"")x&1
}
Dec(x){
   return (StrLen(x)>1 ? Dec(SubStr(x,1,-1))<<1:0)|SubStr(x,0)
}

The fact that they are significantly slower perplexes me. Perhaps it's the overhead of calling itself many times.

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Works great!
PostPosted: February 4th, 2010, 12:55 am 
Offline

Joined: March 18th, 2009, 3:06 pm
Posts: 17
Location: Minnesota, USA
Thanks infogulch, I was looking for something similar about a year ago. I don't remember why I needed it any more though... I think I was working on subnet calculations. Any chance of doing a hex conversion as well? I imagine that may be a little more difficult with the 6 extra characters though. Anyway, great job.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 1:01 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Nice! General base conversion is even faster with dll calls. Converting negative numbers to binary is a bit tricky without specifying the length, but there are many pure AHK solutions out there.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 1:21 am 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
Thanks Laszlo, pkutter. :)

Yes, I just realized this breaks when dealing with negative numbers.

pkutter: for hex numbers try SetFormat

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 6:18 am 
Offline

Joined: March 18th, 2009, 3:06 pm
Posts: 17
Location: Minnesota, USA
Ah, yes.... I had seen that once before... sorry. :oops:


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: MSN [Bot], notsoobvious and 7 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group