Non-decimal radices/Convert: Difference between revisions

Content added Content deleted
m (→‎{{header|Ruby}}: did not provide method to convert from dec to base)
(added Ursala)
Line 894: Line 894:
baseconvert 107h 23 7 ;# ==> 50664
baseconvert 107h 23 7 ;# ==> 50664
baseconvert 50664 7 10 ;# ==> 12345</lang>
baseconvert 50664 7 10 ;# ==> 12345</lang>

=={{header|Ursala}}==
A function parameterized by the base b performs the conversion in each direction.
Folding (=>), iteration (->), and reification (-:) operators among others are helpful.
<lang Ursala>
#import std
#import nat

num_to_string "b" = ||'0'! (-: num digits--letters)*+ @NiX ~&r->l ^|rrPlCrlPX/~& division\"b"

string_to_num "b" = @x =>0 sum^|/(-:@rlXS num digits--letters) product/"b"</lang>
This test program performs the conversions in both directions for a selection of numbers
in base 8 and base 32.
<lang Ursala>test_data = <1,2,15,32,100,65536,323498993>

#cast %sLnLUL

tests =

<
num_to_string32* test_data,
string_to_num32* num_to_string32* test_data,
num_to_string8* test_data,
string_to_num8* num_to_string8* test_data></lang>
output:
<pre>
<
<'1','2','f','10','34','2000','9kgcvh'>,
<1,2,15,32,100,65536,323498993>,
<'1','2','17','40','144','200000','2322031761'>,
<1,2,15,32,100,65536,323498993>></pre>