Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number systems
09-03-2009, 05:56 AM (This post was last modified: 09-15-2009 12:21 AM by drdebcol.)
Post: #1
Number systems
There are some programming tasks that have conversion between different number systems included. That's why i made any to dec and dec to any converter.
I have been doing a bit of research on this and i came to solution.
I made Dec to Bin converter which is pretty simple(http://www.pro9ramming.com/showthread.php?tid=82), but comparing to this "code".
Okay code tells the best about itself, and if you want to find more about converting, you can use wiki.
So code in TPW :
Code:
program number_systems;
uses wincrt;
var
number,option:string;
syst,nu:integer;
label top;
function inttostr(n:longint):string;
var
  i:integer;
  h:array[0..9] of string;
  b:boolean;
  s:string;
begin
h[0]:='0';
h[1]:='1';
h[2]:='2';
h[3]:='3';
h[4]:='4';
h[5]:='5';
h[6]:='6';
h[7]:='7';
h[8]:='8';
h[9]:='9';
b:=false;
s:='';
if n<0 then
begin
n:=-n;
b:=true;
end;
repeat
begin
   s:=h[n mod 10]+s;
   n:=n div 10;
end;
until n=0;
if b=true then
s:='-'+s;
inttostr:=s;
end;
function change_to_dec(numb:string; system:integer):longint;
var
  i,br:integer;
  s:longint;
function power(n,k:longint):longint;
var
  i,p:longint;
begin
  if k=0 then
  begin
  power:=1;

  end
  else
  begin
  p:=1;
  for i:=1 to k do
  p:=p*n;
  power:=p;
  end;
end;
function return_value(s:string):integer;
begin
  if s='0' then
  return_value:=0;
  if s='1' then
  return_value:=1;
  if s='2' then
  return_value:=2;
  if s='3' then
  return_value:=3;
  if s='4' then
  return_value:=4;
  if s='5' then
  return_value:=5;
  if s='6' then
  return_value:=6;
  if s='7' then
  return_value:=7;
  if s='8' then
  return_value:=8;
  if s='9' then
  return_value:=9;
  if s='A' then
  return_value:=10;
  if s='B' then
  return_value:=11;
  if s='C' then
  return_value:=12;
  if s='D' then
  return_value:=13;
  if s='E' then
  return_value:=14;
  if s='F' then
  return_value:=15;
end;
begin
s:=0;
br:=length(numb)-1;
  for i:=1 to length(numb) do
  begin
    s:=s+return_value(numb[i])*power(system,br);
    br:=br-1;
  end;
  change_to_dec:=s;
end;
function dec_to_any(dec:longint;system:integer):string;
var
  s:string;
  function return_value(k:integer):string;
  begin
   if k<10 then
   return_value:=inttostr(k)
   else
     begin
      if k=10 then
      return_value:='A';
      if k=11 then
      return_value:='B';
      if k=12 then
      return_value:='C';
      if k=13 then
      return_value:='D';
      if k=14 then
      return_value:='E';
      if k=15 then
      return_value:='F';
     end;
  end;
begin
  s:='';
  repeat
    begin
     s:=return_value(dec mod system)+s;
     dec:=dec div system;
    end;
  until dec=0;
  dec_to_any:=s;
end;
begin
top:
writeln('Number system converter 1.0');
writeln('Options :');
writeln('1 - Any to Decimal number system');
writeln('2 - Decimal to any number system');
repeat
   begin
    writeln('Enter option :');
    readln(option);
   end;
until (option='1') or (option='2');
if option='1' then
begin
writeln('Enter number :');
readln(number);
writeln('Enter system :');
readln(syst);
writeln('Decimal is :');
writeln(change_to_dec(number,syst));
end
else
begin
writeln('Enter number :');
readln(nu);
writeln('Enter system :');
readln(syst);
writeln('Number is :');
writeln(dec_to_any(nu,syst));
end;
writeln;
writeln('Press Enter to try again !');
readln;
clrscr;
goto top;
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
09-03-2009, 08:31 AM (This post was last modified: 09-03-2009 08:32 AM by G4143.)
Post: #2
RE: Number systems
Here's a question for you. Do you know why functions like

strtol()

only offer number conversions from base 2 to base 36...G4143
Find all posts by this user
Quote this message in a reply
09-03-2009, 05:08 PM
Post: #3
RE: Number systems
I really don't know. Maybe base 36 has something to do with some calculus.
I was really thinking about this issue and i think theoretically that the smallest base of number systems is 2,
so that makes a question "What is the biggest base of number systems ?"
Well decimal system with
Base 10 has 10 digits (0123456789)
Base 11 has one letter (0123456789A)
Base 12 has two letters (0123456789AB)
*
*
*
and so on till end of English Alphabet
which is number of digits + number of letters and that is 36 because
10 digits (0123456789) and 26 letters (A,B,C . . . X,Y,Z)
10+26=36
Maybe that is the answer, i really couldn't find better !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
09-03-2009, 11:42 PM
Post: #4
RE: Number systems
Yep, that's it. The limiting factor is the ten digits 0 - 9 plus the letters a - z....G4143
Find all posts by this user
Quote this message in a reply
09-04-2009, 12:21 AM
Post: #5
RE: Number systems
(09-03-2009 11:42 PM)G4143 Wrote:  Yep, that's it. The limiting factor is the ten digits 0 - 9 plus the letters a - z....G4143
Yeah, that was a fast logical conclusion

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: