Solved

# Base Conversion Program

Posted on 2000-03-16
208 Views
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.
Sollala
0
Question by:sollala
• 2

LVL 5

Expert Comment

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

Author Comment

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

LVL 5

Accepted Solution

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
writeln(convert(s,source,target));
end.

0

## Featured Post

### Suggested Solutions

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…