* * *

Author Topic: Convert a number from X base to decimal without array/string  (Read 651 times)

Bart

  • Hero Member
  • *****
  • Posts: 2718
    • Bart en Mariska's Webstek
Re: Convert a number from X base to decimal without array/string
« Reply #15 on: September 11, 2017, 07:55:58 pm »
My shot at this.
No functions or procedures defined.

Code: Pascal  [Select]
  1. program test;
  2.  
  3. var
  4.   base, digit, converted_to_base_10, divider, temp: LongWord;
  5.   c: char;
  6.   ReadBase: Boolean;
  7.  
  8. begin
  9.   base := 0;
  10.   c := #0;
  11.   converted_to_base_10 := 0;
  12.   ReadBase := True;
  13.   while not eoln(input) do
  14.   begin
  15.     read(input, c);
  16.     if ReadBase then
  17.     begin
  18.       if (c in ['0'..'9']) then
  19.       begin
  20.         digit :=  Ord(c) - Ord('0');
  21.         base :=  10 * base + digit;
  22.       end
  23.       else
  24.       begin
  25.         if (c = ':') then
  26.           ReadBase := False
  27.         else
  28.         begin
  29.           writeln('Illegal input: ',c);
  30.           halt(1);
  31.         end;
  32.       end;
  33.     end
  34.     else
  35.     begin
  36.       if (c in ['0'..'9']) then
  37.       begin
  38.         digit :=  Ord(c) - Ord('0');
  39.       end
  40.       else if (c in ['a'..'z']) then
  41.       begin
  42.         digit := 10 + Ord(c) - Ord('a');
  43.       end
  44.       else if (c in ['A'..'Z']) then
  45.       begin
  46.         digit := 10 + Ord(c) - Ord('A');
  47.       end
  48.       else
  49.       begin
  50.         writeln('Illegal input ',c);
  51.         halt(2);
  52.       end;
  53.       if (digit >= base) then
  54.       begin
  55.         writeln('Ileal input: digit [',c,' -> ',digit,'] cannot be > base - 1 (base = ',base,')');
  56.         halt(3);
  57.       end;
  58.       converted_to_base_10 := digit + (converted_to_base_10 * base);
  59.     end;
  60.   end;
  61.   //writeln('base = ',base);
  62.  
  63.   //Unfortunately we cannot echo back the input, since we were not allowed to store this as a string
  64.   //So we have toconvert "converted_to_base_10" back to base "base" and output the digits one by one
  65.   temp := converted_to_base_10;
  66.   divider := 1;
  67.   while divider < (MaxInt div base) do divider := divider * base;
  68.   while (divider > 0) do
  69.   begin
  70.     digit := temp div divider;
  71.     if (digit < 10) then
  72.       c := Chr(digit + Ord('0'))
  73.     else
  74.       c := Chr(digit - 10 + Ord('A'));
  75.     if digit <> 0 then write(c);
  76.     temp := temp mod divider;
  77.     divider := divider div base;
  78.   end;
  79.   writeln(' (base ',base,') = ',converted_to_base_10,' (base 10)')
  80. end.
  81.  

Outputs:
Code: [Select]
10:123
123 (base 10) = 123 (base 10)

2:11111111
11111111 (base 2) = 255 (base 10)

16:deadbeaf
DEADBEAF (base 16) = 3735928495 (base 10)

Notice I needed a backwards conversion as well to reproduce the input when outputting the result.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 4419
Re: Convert a number from X base to decimal without array/string
« Reply #16 on: September 12, 2017, 09:23:02 am »
I think he means this:
http://www.purplemath.com/modules/numbbase.htm
Excellent write up.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus