Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,66 @@
|
|||
begin
|
||||
% returns with numberInBase set to the number n converted to a string in %
|
||||
% the specified base. Number must be non-negative and base must be in %
|
||||
% range 2 to 36 %
|
||||
procedure convertToBase( integer value n
|
||||
; integer value base
|
||||
; string(32) result numberInBase
|
||||
) ;
|
||||
begin
|
||||
string(36) baseDigits;
|
||||
integer val, strPos;
|
||||
|
||||
assert( n >= 0 and base >= 2 and base <= 36 );
|
||||
|
||||
baseDigits := "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
numberInBase := " ";
|
||||
val := n;
|
||||
strPos := 31;
|
||||
while
|
||||
begin
|
||||
% a(b//c) is the substring of a starting at b with length c. %
|
||||
% The first character is at position 0. The length must be %
|
||||
% an integer literal so it is known at compile time. %
|
||||
numberInBase( strPos // 1 ) := baseDigits( val rem base // 1 );
|
||||
val := val div base;
|
||||
strPos := strPos - 1;
|
||||
val > 0
|
||||
end
|
||||
do begin end
|
||||
end convertToBase ;
|
||||
|
||||
% returns the string numberInBase converted to an integer assuming %
|
||||
% numberInBase ia a string in the specified base %
|
||||
% base must be in range 2 to 36, invalid digits will cause the program %
|
||||
% to crash, spaces are ignored %
|
||||
integer procedure convertFromBase( string(32) value numberInBase
|
||||
; integer value base
|
||||
) ;
|
||||
begin
|
||||
string(36) baseDigits;
|
||||
integer val, cPos;
|
||||
|
||||
assert( base >= 2 and base <= 36 );
|
||||
|
||||
baseDigits := "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
val := 0;
|
||||
for strPos := 0 until 31 do begin
|
||||
string(1) c;
|
||||
c := numberInBase( strPos // 1 );
|
||||
if c not = " " then begin
|
||||
cPos := 0;
|
||||
while baseDigits( cPos // 1 ) not = c do cPos := cPos + 1;
|
||||
val := ( val * base ) + cPos;
|
||||
end
|
||||
end;
|
||||
val
|
||||
end convertFromBase ;
|
||||
|
||||
% test the procedures %
|
||||
string(32) baseNumber;
|
||||
i_w := 3; % set integer output width %
|
||||
for i := 2 until 36 do begin
|
||||
convertToBase( 35, i, baseNumber );
|
||||
write( 35, i, baseNumber, " ", convertFromBase( baseNumber, i ) );
|
||||
end
|
||||
end.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun decimal-to-base-n (number &key (base 16))
|
||||
(format nil (format nil "~~~dr" base) number))
|
||||
|
||||
(defun base-n-to-decimal (number &key (base 16))
|
||||
(read-from-string (format nil "#~dr~d" base number)))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
iex(1)> String.to_integer("ffff", 16)
|
||||
65535
|
||||
iex(2)> Integer.to_string(255, 2)
|
||||
"11111111"
|
||||
iex(3)> String.to_integer("NonDecimalRadices", 36)
|
||||
188498506820338115928429652
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue