in reply to convert numbers to a different (arbitrary) base

#!/usr/bin/perl use strict; use warnings; use Inline 'BC'; # Little languages are cool. sub base { my ($number, $base) = @_; my $r = mybase ($number, $base); # bc returns groups of digits separated by spaces # if the base is larger than 16. # Mangle the return value if the base is at most 36, # but leave it like it is if the base is above 36. if ($base > 16 && $base <= 36) { $r =~ s/([1-9][0-9])/chr(ord('A')+$1-11)/ge; $r =~ s/[0\s]+//g; } $r } use Inline 'BC'; print base(1000000,36), "\n"; __DATA__ __BC__ define mybase(a, b){ obase=b return(a) } KEKR

Replies are listed 'Best First'.
Re^2: convert numbers to a different (arbitrary) base
by hostyle (Scribe) on Oct 21, 2004 at 13:10 UTC

    I can't get that to work. Any pointers?

    skynet [~/bin] 2:07pm>perl alphabc.pl Error. You have specified 'BC' as an Inline programming language. I currently only know about the following languages: C, Foo, foo If you have installed a support module for this language, try deleting + the config file from the following Inline DIRECTORY, and run again: /home/hostyle/bin/_Inline at alphabc.pl line 0 INIT failed--call queue aborted. One or more DATA sections were not processed by Inline. skynet [~/bin] 2:08pm>bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. quit skynet [~/bin] 2:09pm>