Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » Finished MUD Scripts
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Mon Jul 24, 2006 2:01 pm   

Decimal To Binary Convertor
 
After searching through the forums for any decimal to binary conversion function, I decided to make one of my own. No reason to keep it to myself though.


#ALIAS numtobin {#VAR storage = "";#VAR number %1;#if (@number=0) {#ABORT 1};#UNTIL (@number=0) {#if ((@number\2)=1) {storage = %concat( 1, @storage);#MATH number (@number/2)} {storage = %concat( 0, @storage);#MATH number (@number/2)}}}

just call this alias with the parameter being your decimal number, then assign the value of storage to your own variable. Like so:

Code:
#VAR test 1234
numtobin @test
#VAR result @storage
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Mon Jul 24, 2006 10:21 pm   
 
Not bad, but for your next trick actually make it a function with #FUNC and call it with @numtobin(<number to convert>) that way you can use it directly in other aliases and triggers.
_________________
Taz :)
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Mon Jul 24, 2006 10:58 pm   
 
Probably is a way. How to do so is out of my means at the moment. Haven't seen a case where #FUNC has been used for looping in anything other than a recursive function.
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Tue Jul 25, 2006 10:50 am   
 
Exactly, and this is well suited to recursion.
Quote:
#FUNC dec2bin {%if("%1"!="0",%concat(@dec2bin(%eval(%1/2)),%mod(%1,2)))}
_________________
Taz :)
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Tue Jul 25, 2006 12:24 pm   
 
ugh, that is exactly how I was picturing it but for some reason I kept getting hung up on the generic case of "%1" being equal to 0 in the first place, not realising I could just have it so that the variable this would get stored in had a default value of 0.
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Tue Jul 25, 2006 12:52 pm   
 
Ok so this same method can be expanded to include other bases. I'll give you base2 to base9 see if you can extend it to handle base10 to base16, I've got the answer it really isn't pretty so you might come up with something better.
Quote:
#FUNC dec2any {%if("%1"!="0",%concat(@dec2any(%eval(%1/%2),%2),%mod(%1,%2)))}
Note now that the base is passed in to the function so @dec2any(127,2) will give you 1111111 and @dec2any(127,8) will give you 177.

Since you know I'll be testing with @dec2any(127,16) don't hard code it to give you 7F either Wink Laughing
_________________
Taz :)
Reply with quote
edb6377
Magician


Joined: 29 Nov 2005
Posts: 482

PostPosted: Tue Jul 25, 2006 2:40 pm   
 
Taz is hard at work teaching again :P
_________________
Confucious say "Bugs in Programs need Hammer"
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Tue Jul 25, 2006 3:14 pm   
 
Aye! Very Happy
_________________
Taz :)
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Tue Jul 25, 2006 3:40 pm   
 
I have some training in programming languages, but obviously zMUD scripting is a little different. Have to do things in odd ways sometimes to get what you want. Obviously, to go to base 16 you'd have to have a way to convert 11-16 into A-F.
Reply with quote
Tarn
GURU


Joined: 10 Oct 2000
Posts: 853
Location: USA

PostPosted: Tue Jul 25, 2006 5:28 pm   
 
Taz wrote:

extend it to handle base10 to base16, I've got the answer it really isn't pretty


Dumas wrote:
I have some training in programming languages, but obviously zMUD scripting is a little different. Have to do things in odd ways sometimes to get what you want. Obviously, to go to base 16 you'd have to have a way to convert 11-16 into A-F.


Correction: you map 10-15 to A-F (a hexadecimal digit represents a number between 0 and 15 inclusive, just as a decimal digit represents a number between 0 and 9 inclusive).

Or you could just use the VB script function "Hex" to go straight from base 10 to a base 16 string.

If you want a pure zScript solution, the most obvious way would be to pull groups of 4 bits from the end of your binary representation and use a lookup table to get to the hex digits.

-Tarn
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Tue Jul 25, 2006 5:31 pm   
 
Quote:
#FUNC dec2any {%if( "%1"!="0", %concat( @dec2any(%eval(%1/%2),%2), %item( {0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z}, (%mod( %1, %2)+1))))}


Works to base36 Twisted Evil
Do I get a gold star?
Reply with quote
Vodoc
Apprentice


Joined: 11 Apr 2003
Posts: 119
Location: Sweden

PostPosted: Tue Jul 25, 2006 5:38 pm   
 
Bah to late but here is my solution using two functions (also works from base 2 to 36):
Code:
#FUNC dec2any {%if( "%1"!="0", %concat( @dec2any(%eval(%1/%2),%2), @dec2any_translate(%mod( %1, %2))))}
#FUNC dec2any_translate {%if( %1 > 9, %char( 55+%1), %1)}


Edit:

And here is the any2dec function, I leave it to you to create the any2any function which should be easy by now Smile
Quote:
#FUNC pow {%if( %2 > 0, %eval( %1 * @pow(%1, %eval(%2-1))), 1)}
#FUNC any2dec {%if(%len(%1)>0,%eval(%eval(%ismember(%left(%1, 1),{0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z})-1)*@pow(%2,%eval(%len(%1)-1))+@any2dec(%right(%1,1),%2)),0)}
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Wed Jul 26, 2006 12:31 am   
 
See, it is things like this that make me confoozled sometimes. I could probably come up with this, but there are so many oddball things going on that just looking at this gives me a headache. Very Happy
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Wed Jul 26, 2006 1:32 pm   
 
Yes gold star to Guinn, far more elegant than my solution and nicely extended.

Gold star to Vodoc for the any2dec function.

I used to have a whole class of mathematical functions called mathfun but seem to have lost it in all the computer moves I've done, perhaps this will spur me to recreate it.

And I still love the fact there are so many ways of acheiving a goal in zScript.
_________________
Taz :)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » Finished MUD Scripts All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net