Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.ascii, std.array;
|
||||
import std.stdio, std.algorithm, std.ascii, std.array, std.string;
|
||||
|
||||
alias Digits = ubyte[];
|
||||
|
||||
Digits toBase(ulong number, in ubyte base) pure nothrow {
|
||||
Digits toBase(ulong number, in ubyte base) pure nothrow @safe {
|
||||
Digits result;
|
||||
while (number) {
|
||||
result = number % base ~ result;
|
||||
|
|
@ -11,19 +11,19 @@ Digits toBase(ulong number, in ubyte base) pure nothrow {
|
|||
return result;
|
||||
}
|
||||
|
||||
enum fromBase = (in Digits digits, in ubyte base) pure nothrow =>
|
||||
enum fromBase = (in Digits digits, in ubyte base) pure nothrow @safe @nogc =>
|
||||
reduce!((n, k) => n * base + k)(0UL, digits);
|
||||
|
||||
immutable myDigits = digits ~ lowercase;
|
||||
|
||||
enum fromDigits = (in Digits digits) pure nothrow =>
|
||||
enum fromDigits = (in Digits digits) pure nothrow /*@safe*/ =>
|
||||
digits.map!(d => myDigits[d]).array;
|
||||
|
||||
enum convert = (in dchar d) pure nothrow =>
|
||||
cast(ubyte)(d.isDigit ? d - '0' : d.toLower - 'a' + 10);
|
||||
enum convert = (in dchar d) pure nothrow @safe @nogc =>
|
||||
cast(ubyte)(d.isDigit ? d - '0' : std.ascii.toLower(d) - 'a' + 10);
|
||||
|
||||
enum toDigits = (in string number) pure /*nothrow*/ =>
|
||||
number.map!convert.array;
|
||||
enum toDigits = (in string number) pure nothrow @safe =>
|
||||
number.representation.map!convert.array;
|
||||
|
||||
void main() {
|
||||
"1ABcd".toDigits.fromBase(16).writeln;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
use Math::BaseCnv 'cnv';
|
||||
print cnv("1a", 16, 10),"\n"; # "1a" from hex to decimal prints 26
|
||||
print lc(cnv(26, 10, 16)),"\n"; # 26 from decimal to hex prints "1a"
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
┌────────────────────────────────────────────────────────────────────┐
|
||||
┌─┘ Input to this program (bases must be positive integers > 1): └─┐
|
||||
│ │
|
||||
│ x is required (it may have a sign). │
|
||||
│ toBase the base to convert X to. │
|
||||
│ inBase the base X is expressed in. │
|
||||
│ │
|
||||
│ If X has a leading sign, it is maintained (kept) after conversion. │
|
||||
│ │
|
||||
│ toBase or inBase can be a comma (,) which causes the default │
|
||||
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/*REXX pgm converts integers from one base to another (base 2 ──► 90). */
|
||||
@abc = 'abcdefghijklmnopqrstuvwxyz' /*the lowercase (Latin) alphabet.*/
|
||||
parse upper var @abc @abcU /*uppercase a version of @abc. */
|
||||
@@ = 0123456789 || @abc || @abcU /*prefix 'em with numeric digits.*/
|
||||
@@ = @@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
|
||||
/* [↑] all chars must be viewable*/
|
||||
numeric digits 3000 /*what da hey, support gihugeics.*/
|
||||
maxB=length(@@) /*max base (radix) supported here*/
|
||||
parse arg x toB inB 1 ox . 1 sigX 2 x2 . /*get: 3 args, origX, sign···*/
|
||||
if pos(sigX,"+-")\==0 then x=x2 /*Does X have a leading sign? */
|
||||
else sigX= /*Nope. No leading sign for X. */
|
||||
if x=='' then call erm /*if no X number, issue error.*/
|
||||
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/
|
||||
if inB=='' | inB=="," then inB=10 /* " " " " " */
|
||||
if inB<2 | inb>maxB | \datatype(inB,'W') then call erb 'inBase ' inB
|
||||
if toB<2 | toB>maxB | \datatype(toB,'W') then call erb 'toBase ' toB
|
||||
#=0 /*result of converted X (base 10)*/
|
||||
do j=1 for length(x) /*convert X, base inB ──► base 10*/
|
||||
?=substr(x,j,1) /*pick off a numeral/digit from X*/
|
||||
_=pos(?, @@) /*calculate this numeral's value.*/
|
||||
if _==0 | _>inB then call erd x /*_ character an illegal numeral?*/
|
||||
#=#*inB+_-1 /*build a new number, dig by dig.*/
|
||||
end /*j*/ /* [↑] this also verifies digits*/
|
||||
y= /*the value of X in base B. */
|
||||
do while # >= toB /*convert #, base 10 ──► base toB*/
|
||||
y=substr(@@, (#//toB)+1, 1)y /*construct the output number. */
|
||||
#=#%toB /*··· and whittle # down also. */
|
||||
end /*while*/ /* [↑] process leaves a residual*/
|
||||
/* [↓] Y is the residual*/
|
||||
y=sigX || substr(@@, #+1, 1)y /*prepend the sign if it existed.*/
|
||||
say ox "(base" inB')' center('is',20) y "(base" toB')'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────one─liner subroutines───────────────*/
|
||||
erb: call ser 'illegal' arg(1)", it must be in the range: 2──►"maxB
|
||||
erd: call ser 'illegal digit/numeral ['?"] in: " x
|
||||
erm: call ser 'no argument specified.'
|
||||
ser: say; say '***error!***'; say arg(1); exit 13
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
/*REXX program converts numbers from one base to another, from 2 ──► 90.*/
|
||||
/*┌────────────────────────────────────────────────────────────────────┐
|
||||
┌─┘ Input to this program (bases must be positive whole numbers): └─┐
|
||||
│ │
|
||||
│ x is required (it may have a sign). │
|
||||
│ toBase the base to convert X to. │
|
||||
│ inBase the base X is expressed in. │
|
||||
│ │
|
||||
│ toBase or inBase can be a comma (,) which causes the default │
|
||||
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘
|
||||
└────────────────────────────────────────────────────────────────────┘*/
|
||||
@abc='abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */
|
||||
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
|
||||
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/
|
||||
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
|
||||
/*special chars must be viewable.*/
|
||||
numeric digits 1000 /*what da hey, support gihugeics.*/
|
||||
maxB=length(@@@) /*max base (radix) supported here*/
|
||||
parse arg x toB inB 1 ox . /*get a number, toBase, inBase*/
|
||||
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/
|
||||
if inB=='' | inB==',' then inB=10 /* " " " " " */
|
||||
if inB<2 | inb>maxB then call erb 'inBase',inB /*bad boy inBase.*/
|
||||
if toB<2 | tob>maxB then call erb 'toBase',toB /* " " toBase.*/
|
||||
if x=='' then call erm /* " " number.*/
|
||||
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) /*X has sign?*/
|
||||
else sigX= /*no sign. */
|
||||
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/
|
||||
_=substr(x,j,1) /*pick off a "digit" from X. */
|
||||
v=pos(_,@@@) /*get the value of this "digit". */
|
||||
if v==0 | v>inB then call erd x,j,inB /*illegal "digit"? */
|
||||
#=#*inB+v-1 /*construct new num, dig by dig. */
|
||||
end /*j*/
|
||||
|
||||
y=; do while # >= toB /*convert #, base 10 ──► base toB*/
|
||||
y=substr(@@@,(#//toB)+1,1)y /*construct the output number. */
|
||||
#=#%toB /*... and whittle # down also. */
|
||||
end /*while #>-toB*/
|
||||
|
||||
y=sigX || substr(@@@,#+1,1)y /*prepend the sign if it existed.*/
|
||||
say ox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────error subroutines───────────────────*/
|
||||
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" maxB
|
||||
erd: call ser; say 'illegal "digit" in' x":" _
|
||||
erm: call ser; say 'no argument specified.'
|
||||
ser: say; say '*** error! ***'; say; exit 13
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class String
|
||||
def convert_base(from, to)
|
||||
Integer(self, from).to_s(to)
|
||||
# self.to_i(from).to_s(to) #if you don't want exceptions
|
||||
end
|
||||
end
|
||||
|
||||
# first three taken from TCL
|
||||
p "12345".convert_base(10, 23) # => "107h"
|
||||
p "107h".convert_base(23, 7) # =>"50664"
|
||||
p "50664".convert_base(7, 10) # =>"12345"
|
||||
p "1038334289300125869792154778345043071467300".convert_base(10, 36) # =>"zombieseatingdeadvegetables"
|
||||
p "ff".convert_base(15, 10) # => ArgumentError
|
||||
Loading…
Add table
Add a link
Reference in a new issue