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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Gilbert
Beginner


Joined: 10 Oct 2000
Posts: 11
Location: Germany

PostPosted: Sun May 23, 2004 10:43 am   

Request: DEC2HEX
 
Hi there,
it is possible to add this function?

Or anyone else can help me, to get this work [?]

Regaards,
Christian - going crazy Very Happy
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Sun May 23, 2004 11:46 am   
 
quote:
Originally posted by Gilbert

Hi there,
it is possible to add this function?

Or anyone else can help me, to get this work [?]

Regaards,
Christian - going crazy Very Happy



Hello,

Adding hex support isn't too difficult. Here is how to do it:

#VAR hex {0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F}
#VAR toHex {%if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")}

Using this you could add binary support too:
#VAR toBin {%if( %1<>0, %concat( @toBin(%eval(%1 / 2)), %item( @bin, %eval( %mod( %1, 2)+1))), "")}
#VAR bin {0|1}

Example:
#echo @toHex(20)
> 14

#echo @toHex(2000)
> 7D0

#echo @toBin(20)
> 10100
Reply with quote
Gilbert
Beginner


Joined: 10 Oct 2000
Posts: 11
Location: Germany

PostPosted: Sun May 23, 2004 12:00 pm   
 
Hi Rorso,
many many thanx to you. i think, i should think easier....

Christian
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Sun May 23, 2004 5:15 pm   
 
Should use the #FUNCTION command for this.
#VAR toHex {%if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")}
#VAR toHex
Displays:
Variable: toHex ............. @toHex(%eval(%1 / 16))0

#FUN toHex {%if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")}
#VAR toHex
Displays:
Variable: toHex ............. %if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")

Nice work on the functions.
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Sun May 23, 2004 5:44 pm   
 
quote:
Originally posted by LightBulb

Should use the #FUNCTION command for this.
#VAR toHex {%if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")}
#VAR toHex
Displays:
Variable: toHex ............. @toHex(%eval(%1 / 16))0

#FUN toHex {%if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")}
#VAR toHex
Displays:
Variable: toHex ............. %if( %1<>0, %concat( @toHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")


Ah so that is the difference between #func and #var Very Happy.

quote:

Nice work on the functions.


Thanks. I discovered an annoying bug though:
@toHex(0) -> ""

It happens because the end case of the recursion has to be the empty string or you get 0 appended to the outcome. Luckily that is easy to solve using a wrapper function.

Func: toHex
code: %if( %1<>0, @realToHex(%1), 0)

Func: realToHex
code: %if( %1<>0, %concat( @realToHex(%eval(%1 / 16)), %item( @hex, %eval( %mod( %1, 16)+1))), "")

#echo @toHex(0) -> 0
Reply with quote
Guede
Wanderer


Joined: 30 Nov 2003
Posts: 65
Location: United Kingdom

PostPosted: Sun May 23, 2004 7:08 pm   
 
Just as a side note here, if you are confident with COM scripting (which I'm not) and VB Script you could use the VBScript Hex() function to do it for you.

-G-
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Sun May 23, 2004 10:26 pm   
 
quote:
Originally posted by Guede

Just as a side note here, if you are confident with COM scripting (which I'm not) and VB Script you could use the VBScript Hex() function to do it for you.

-G-


That's clever. You can use the %mss function to call a VBScript function like this:
#echo %mss("hex(4000)", "vbscript")
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion 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