AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to convert an integer from decimal to Octal?!

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
New_G
Guest





PostPosted: Sat Apr 02, 2005 7:33 am    Post subject: How to convert an integer from decimal to Octal?! Reply with quote

hi,
I need to convert an integer from decimal to octal

is that possible by using ahk?!

thanks for help!
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Sat Apr 02, 2005 12:14 pm    Post subject: Reply with quote

Not directly, you would have to write a subroutine that does it mathematically.
Back to top
View user's profile Send private message Send e-mail
lost



Joined: 08 Mar 2005
Posts: 10

PostPosted: Sat Apr 02, 2005 3:35 pm    Post subject: Reply with quote

Hi,

Code:
inputbox,cn
ocn=
loop
{
   transform,x,mod,cn,8
   stringleft,x,x,1
   ocn=%x%%ocn%
   cn:=(cn-x)/8
   if cn<8
   {
      stringleft,cn,cn,1
      ocn=%cn%%ocn%
      break
   }
}
msgbox %ocn%
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Sat Apr 02, 2005 3:38 pm    Post subject: Reply with quote

Snazzy!
Back to top
View user's profile Send private message Send e-mail
BoBo
Guest





PostPosted: Sat Apr 02, 2005 7:34 pm    Post subject: Reply with quote

But he's lost ! Laughing
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Sat Apr 02, 2005 10:27 pm    Post subject: Reply with quote

Nice!

Minor issue: single digit results have a 0 prefix, others not. We can fix it and simplify the loop slightly:
Code:
Loop
{
   Transform x, mod, cn, 8
   ocn = %x%%ocn%
   Transform cn, BitShiftRight, cn, 3
   IfLess cn,1, break
}
Negative numbers are not converted correctly, but at least, there is no infinite loop. If you need negative octals, insert the following in front of the Loop
Code:
IfLess cn,0, {
   sign = -
   cn := -cn
}
The result is
Code:
%sign%%ocn%
Back to top
View user's profile Send private message
lost



Joined: 08 Mar 2005
Posts: 10

PostPosted: Sun Apr 03, 2005 4:33 am    Post subject: Reply with quote

Hi,
Laszlo
Quote:

Minor issue: single digit results have a 0 prefix, others not


yes .. I changed the order of the code without changing the code itself
I hope that could fix it..! thanks
Code:

inputbox,cn
ocn=
loop
{
   if cn<8
   {
      stringleft,cn,cn,1
      ocn=%cn%%ocn%
      break
   }
   transform,x,mod,cn,8
   stringleft,x,x,1
   ocn=%x%%ocn%
   cn:=(cn-x)/8

}
msgbox %ocn%


Laszlo
Quote:

Negative numbers are not converted correctly


In fact I didn't try any negative numbers. but there 're many ways to fix this bug if anyone needs to use negative values.

Tthanks
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group