Leser: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
use 5.012; use List::Util qw[reduce]; sub str2num { my ($str, $base) = @_; reduce { $a * $base + $b } 0, split //, $str; } sub num2str { my ($num, $base) = @_; if ($num > 0) { num2str(int($num / $base), $base) . ($num % $base); } else { '0'; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
use strict; my $Num=12345; my $System=36; my $String = toString($Num,$System); print $String."\n"; my $Zahl=parseInt($String,$System); print $Zahl; sub toString{ my $zahl=shift; my $base=shift; my $out=""; my $rest; my @werte=qw(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); while ($zahl > 0){ $rest=$zahl % $base; $out.=$werte[$rest]; $zahl=int($zahl / $base); } $out = reverse($out); return $out; } sub parseInt (){ my $str=shift; my $base=shift; $str=reverse($str); my $val=""; my $back=0; my %wert; my @werte=qw(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); for ( my $x =0 ; $x <=35; $x++) { $wert{$werte[$x]}=$x } for ( my $x =0 ; $x <=length($str); $x++) { $val=substr($str,$x,1); $back += $wert{$val} * ($base ** $x); } return $back; }
for ( my $x =0 ; $x < length($str); $x++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/env python #-*- coding: iso-8859-1 -*- def toString(zahl, base): werte = ('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') out = "" while (zahl > 0): rest = zahl % base out += werte[rest] zahl = zahl // base return out[::-1] def parseInt(str_, base): werte = ('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') wert = {} str_ = str_[::-1] val = "" back = 0 for x in range(36): wert[werte[x]] = x for x in range(len(str_)): val_ = str_[x:x + 1] back += wert[val_] * (base ** x) return back num = 12345 system_ = 36 string_ = toString(num, system_) print string_ zahl = parseInt(string_, system_) print zahl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
our @VAL = ( 0 .. 9, 'a' .. 'z' ); # edit our %VAL = map { $VAL[$_] => $_ } 0 .. $#VAL; sub parseInt { my( $str, $base, $int ) = @_; $int ||= 0; $str =~ /^(.)(.*)$/ ? parseInt( $2, $base, $int * $base + $VAL{lc $1} ) : $int; } # parseInt sub toString { my( $int, $base ) = @_; $int > 0 ? (toString( int($int/$base), $base ) || '') . $VAL[$int % $base] : '0'; # edit } # toString