Solved

Base Conversion Program

Posted on 2000-03-16
3
212 Views
Last Modified: 2010-04-16
Hello
I am looking for a program which can convert Base type of given number.
Like convert hex to decimal, octal to hex, hex to octal,bin to hex etc..
A DOS version of program is OK.
Thanks in advance
Sollala
0
Comment
Question by:sollala
  • 2
3 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 2626267
The Calculator that ships with Windows is capable of this.  In the View menu, selected Scientific.
0
 

Author Comment

by:sollala
ID: 2626292
I know that calculator can do that, I want a Pascal Program which does this.
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 2626396
well, I just threw this program together, you can extract from it what you need


The function "convert" accepts three arguments:  the source number (in a string), the source base (1-16), and the target base(1-16).

It will between any of these (e.g. binary to octal, octal to hex, base 3 to base 15, etc., etc.) for a total of 256 possible conversions.

For example, to convert "123" decimal to octal, you would use the expression

convert('123',10,8);


*********************************


program base_converter;


const
  digits :string[16] = '0123456789ABCDEF';

function convert_digit(c :char) :integer;
var
  digit :integer;
begin
  digit := ord(upcase(c));
  if digit in [ord('0')..ord('9')] then
    digit := digit - ord('0')
  else if digit in [ord('A')..ord('F')] then
    digit := digit - ord('A') + 10
  else
    digit := -1;
  convert_digit := digit;
end;



function convert(s :string; source, target :integer) :string;
var
  decimal, place :longint;
  i,digit,d :integer;
  s2 :string;
  msd :boolean;
  error :boolean;
begin
  place := 1;
  decimal := 0;
  s2 := '';
  i := length(s);
  error := false;
  while (i > 0) and (not error) do begin
    digit := convert_digit(s[i]);
    if (digit < 0) or (digit >= source) then
      error := true
    else begin
      decimal := decimal + place * digit;
      place := place * source;
      dec(i);
    end;
  end;
  if not error then begin
    msd := false;
    place := 1;
    while place < decimal do place := place * target;
    while place > 0 do begin
      d := decimal div place;
      if (d <> 0) or msd then begin
        s2 := s2 + digits[d+1];
        msd := true;
      end;
      decimal := decimal mod place;
      place := place div target;
    end;
  end else
    s2 := '**ERROR: INVALID CHARACTER***';
  convert := s2;
end;

var
  source,target :integer;
  s :string;
begin
  write('Enter source base(1-16):'); readln(source);
  write('Enter target base(1-16):'); readln(target);
  write('Enter value:'); readln(s);
  writeln(convert(s,source,target));
  readln;
end.





0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TMemoryStream -> PNGImage -> TIcon convertion 22 1,226
Converting a Windows Bitmap Stream to TIcon 2 800
Delphi TSQL Connection runtime creating 1 1,080
Lazarus and Excel formulas via OLE 7 246
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now