Decimal to Binary

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Decimal to Binary

Post by JRL » Thu May 15, 2014 4:19 am

This seems to work. Anyone know an easier way?

Code: Select all

//Decimal to Binary by JRL
//May 14, 2014
  Let>DecVal=95298
  Let>BinaryNumber={%DecVal% mod 2}
  Let>res={int(%DecVal%/2)}
Repeat>res
  Let>Bin={%res% mod 2}
  Let>BinaryNumber=%bin%%BinaryNumber%
  Let>res={int(%res%/2)}
Until>res<2
  Let>Bin={%res% mod 2}
  Let>BinaryNumber=%bin%%BinaryNumber%
  //Let>BinaryNumber=%BinaryNumber%*1  /*removes leading zeros*/
  
  MDL>Decimal = %DecVal%%crlf%Binary = %BinaryNumber%

User avatar
Djek
Pro Scripter
Posts: 147
Joined: Sat Feb 05, 2005 11:35 pm
Location: Holland
Contact:

Re: Decimal to Binary

Post by Djek » Thu May 15, 2014 2:29 pm

hi JRL
It does not work correctly

if you change the line
Until>res<2
into
Until>res<3

it does.
kind regards
Djek

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Decimal to Binary

Post by JRL » Thu May 15, 2014 2:41 pm

Thank you Djek.

I added the line to remove leading zeros this morning without testing it. :oops: The multiplication on a large number converts the number to scientific notation. I changed the original post remarking out that line so it should work now. Let me know if I'm wrong.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Decimal to Binary

Post by Marcus Tettmar » Thu May 15, 2014 3:39 pm

A bit shorter:

Code: Select all

Let>DecVal=95298
Let>BinVal=
Repeat>DecVal
 Let>tmp={%DecVal% mod 2}
 Let>BinVal=%tmp%%BinVal%
 Let>DecVal={%DecVal% div 2}
Until>DecVal=0
MessageModal>BinVal
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Decimal to Binary

Post by JRL » Thu May 15, 2014 4:28 pm

Excellent!
Thank you.
:D

Here it is set up as an includable subroutine.

Code: Select all

//DecToBin Usage:
//GoSub>DecToBin,Decimal Number
//Returns binary result in variable "BinVal"

SRT>DecToBin
  Let>BinVal=
  Repeat>DecToBin_var_1
   Let>tmp={%DecToBin_var_1% mod 2}
   Let>BinVal=%tmp%%BinVal%
   Let>DecToBin_var_1={%DecToBin_var_1% div 2}
  Until>DecToBin_var_1=0
END>DecToBin

User avatar
Djek
Pro Scripter
Posts: 147
Joined: Sat Feb 05, 2005 11:35 pm
Location: Holland
Contact:

Re: Decimal to Binary

Post by Djek » Thu May 15, 2014 7:39 pm

wel i think Marcus is the best,

My script especially made for you JRL, i find easier to understand;

Code: Select all

Let>DecVal=95298
let>TotalBits=16
let>binval=
let>decval2=decval+1
repeat>TotalBits
  if>%decval2%>{2^%TotalBits%}
    let>binval=%binval%1
    let>decval2=%decval2%-{2^%TotalBits%}
   else
    let>binval=%binval%0
  endif
  let>TotalBits=TotalBits-1
until>TotalBits<0
MessageModal>Decimal = %Decval%%CRLF%Binary = %binval%

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Decimal to Binary

Post by JRL » Thu May 15, 2014 8:36 pm

wel i think Marcus is the best,
Marcus is always the best!

Good job on the alternate method. Maybe I should have saved this and rather than posting made a puzzler out of it. Maybe could have gotten several more possible ways.

Hmmm. I have an idea

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts