1. Technology

How to convert numbers from one base to another

By

A number system defines a set of values used to represent quantity. Computer uses the binary system. Understanding binary and hexadecimal numbers (often used to represent values [numbers and memory addresses] in computer systems) is essential for systems-level programming. Binary numbers are important because the computer "works with" binary numbers -- numbers composed of two digits, 1 and 0. Hexadecimal numbers are convenient because they allow us to handle binary numbers easily.

Delphi provides many useful functions for converting integer (decimal) values to strings (way to represent hexadecimal and binary numbers) and vice versa.

Let's see what functions we can use to convert numbers from one base to another, what functions are missing and how to easily implement them in Object Pascal. IntToHex, HexToInt
In the SysUtils unit there is an IntToHex function that returns the hex representation of an integer.

function IntToHex (Value: Integer; Digits: Integer): string;

Ok, we have the ability to convert integer to hexadecimal, but where is the HexToInt function? There is no HexToInt function in Delphi??.
Nevertheless, Delphi allows expressions with hexadecimal notation (using a $ prefix). So, here is a simple HexToInt function:

~~~~~~~~~~~~~~~~~~~~~~~~~
function HexToInt(HexNum: string): LongInt;
begin
   Result:=StrToInt('$' + HexNum) ;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~

IntToBin, BinToInt
First, to be more familiar with binary numbers, why not read: An Introduction to Binary Arithmetic.
Like HexToInt, there are no IntToBin and BinToInt functions in Delphi. Therefore, here they are:

~~~~~~~~~~~~~~~~~~~~~~~~~
function IntToBin ( value: LongInt; digits: integer ): string;
begin
    result := StringOfChar ( '0', digits ) ;
    while value > 0 do begin
      if ( value and 1 ) = 1 then
        result [ digits ] := '1';
      dec ( digits ) ;
      value := value shr 1;
    end;
end;

function BinToInt(Value: String): LongInt;
var i: Integer;
begin
  Result:=0;
//remove leading zeroes
  while Copy(Value,1,1)='0' do
   Value:=Copy(Value,2,Length(Value)-1) ;
//do the conversion
  for i:=Length(Value) downto 1 do
   if Copy(Value,i,1)='1' then
    Result:=Result+(1 shl (Length(Value)-i)) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Romans
The number system in most common use today is the Arabic system. The ten single-digit numerals, "0" through "9", make up the symbols of our numbering system. An example of a different numeral system is the system of Roman numerals, which could represent all the numbers from 1 to 1,000,000 using only seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Example: 1999 (Arabic) is MCMXCIX (Roman).

~~~~~~~~~~~~~~~~~~~~~~~~~
function IntToRoman(Value: LongInt): String;
const
Arabics: Array[1..13] of Integer = 1,4,5,9,10,40,50,90,100,400,500,900,1000) ;
Romans: Array[1..13] of String = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M') ;
var
   j: Integer;
begin
  for j := 13 downto 1 do
  while (Value >= Arabics[j]) do begin
   Value := Value - Arabics[j];
   Result := Result + Romans[j];
  end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

  1. About.com
  2. Technology
  3. Delphi
  4. Coding Delphi Applications
  5. How to Convert Numbers From One Base to Another

©2014 About.com. All rights reserved.