Convert binary to decimal

 

Can someone give me an idea of a simple way to convert binary (base2) to

decimal(base10).

Solution 1

[Anatoly Podgoretsky, kvk@estpak.ee]

 


////////////////////////////////////////////////

// convert 32 bit base2 to 32 bit base10      //

// max number = 99 999 999, return -1 if more //

////////////////////////////////////////////////

 

function Base10(Base2:Integer) : Integer; assembler;

asm

  cmp    eax,100000000            // check upper limit

  jb     @1                       // ok

  mov    eax,-1                   // error flag

  jmp    @exit                    // exit with -1

@1:

  push   ebx                      // save registers

  push   esi

  xor    esi,esi                  // result = 0

  mov    ebx,10                   // diveder base 10

  mov    ecx,8                    // 8 nibbles (10^8-1)

@2:

  mov    edx,0                    // clear remainder

  div    ebx                      // eax DIV 10, edx mod 10

  add    esi,edx                  // result = result + remainder[I]

  ror    esi,4                    // shift nibble

  loop  @2                       // loop for all 8 nibbles

  mov    eax,esi                  // function result

  pop    esi                      // restore registers

  pop    ebx

@exit:

end;

 


Solution 2

[Oliver Townshend, oliver@zip.com.au]

 


function IntToBin(Value: LongInt;Size: Integer): String;

var

        i: Integer;

begin

        Result:='';

        for i:=Size downto 0 do begin

                if Value and (1 shl i)<>0 then begin

                        Result:=Result+'1';

                end else begin

                        Result:=Result+'0';

                end;

        end;

end;

 

function BinToInt(Value: String): LongInt;

var

        i,Size: Integer;

begin

        Result:=0;

        Size:=Length(Value);

        for i:=Size downto 0 do begin

                if Copy(Value,i,1)='1' then begin

                        Result:=Result+(1 shl i);

                end;

        end;

end;

 


Solution 3

[Demian Lessa, knowhow@compos.com.br]

Give this function any decimal value, specify a base (1..16) and it will return you a string containing the proper value, BaseX. You can use a similar method for Arabic/Roman conversion (see below).

 


function DecToBase( Decimal: LongInt; const Base: Byte): String;

const

  Symbols: String[16] = '0123456789ABCDEF';

var

  scratch: String;

  remainder: Byte;

begin

  scratch := '';

  repeat

    remainder := Decimal mod Base;

    scratch := Symbols[remainder + 1] + scratch;

    Decimal := Decimal div Base;

  until ( Decimal = 0 );

  Result := scratch;

end;

 


Give this function any decimal value (1...3999), and it will return you a string containing the proper value in Roman notation.

 


function DecToRoman( Decimal: LongInt ): String;

const

  Romans:  Array[1..13] of String =

    ( 'I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M' );

 

  Arabics: Array[1..13] of Integer =

    ( 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);

 

var

  i: Integer;

  scratch: String;

begin

  scratch := '';

  for i := 13 downto 1 do

    while ( Decimal >= Arabics[i] ) do

    begin

      Decimal := Decimal - Arabics[i];

      scratch := scratch + Romans[i];

    end;

  Result := scratch;

end;