Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Binary to decimal and Decimal to binary conversion

darscute03darscute03 Posts: 12Member
can anyone help me with this binary to decimal numbers then another one is decimal to binary tnx plus if you know something about binary to octal please help thanks

Comments

  • AtexAtex Posts: 268Member
    : can anyone help me with this binary to decimal numbers then another
    : one is decimal to binary tnx plus if you know something about binary
    : to octal please help thanks
    :

    Decimal to binary ( and vice versa ) + binary to octal ( and vice versa ) demo (not the nicest code but works):[code][color=Blue]program dec_bin_oct;

    const po2:array[0..7] of byte=(1,2,4,8,16,32,64,128); {powers of 2}
    bin:array[false..true] of char='01';
    oct:array[0..7] of char='01234567';

    {Decimal to binary}
    function dec2bin(b:byte):string;
    var s:string;
    i:byte;
    begin
    s:='';
    for i:=7 downto 0 do
    s:=s+bin[(po2[i] and b=po2[i])];
    dec2bin:=s;
    end;

    {Binary ot decimal}
    function bin2dec(s:string):byte;
    var l:byte absolute s; {lenght of s}
    i,b:byte;
    begin
    b:=0;
    while l<8 do s:=bin[false]+s;
    if l>8 then s:=copy(s,l-7,8);
    for i:=1 to l do
    if s[i]=bin[true] then b:=b+po2[8-i];
    bin2dec:=b;
    end;

    {Binary to octal}
    function bin2oct(b:string):string;
    var s:string;
    l:byte absolute b; {lenght of b}
    i:byte;
    begin
    while ((l mod 3)<>0) do b:=bin[false]+b;
    s:='';
    for i:=1 to (l div 3) do
    s:=s+oct[bin2dec(b[pred(i)*3+1]+b[pred(i)*3+2]+b[pred(i)*3+3])];
    bin2oct:=s;
    end;

    {Octal to binary}
    function oct2bin(b:string):string;
    var l:byte absolute b;
    i:byte;
    s:string;
    begin
    s:='';
    for i:=1 to l do
    s:=s+copy(dec2bin(ord(b[i])-48),6,3);
    oct2bin:=s;
    end;


    var n:byte;

    begin
    write(#13#10,'Enter a decimal :');readln(n);
    writeln(n,' in binary :',dec2bin(n),', in decimal :',bin2dec(dec2bin(n)));
    writeln(n,' in octal :',bin2oct(dec2bin(n)),' in binary: ',oct2bin(bin2oct(dec2bin(n))));
    readln;
    end.
    [/color][/code]
  • darscute03darscute03 Posts: 12Member
    haha tnx but can you explain me every line hahaha im little confuse about the program but it works i just cant explain how ...
  • darscute03darscute03 Posts: 12Member
    can you help me on this?how does this work?
  • ActorActor Posts: 438Member
    All data is stored in the computer in some sort of binary format. I assume that what you need is a way to convert that binary representation into a string of 1s and 0s representing the same thing. Converting integers to/from (machine) binary from/to a decimal string representation is accomplished in Pascal using the procedures [b]Str[/b] and [b]Val[/b]. This unit defines [b]BinStr[/b] and [b]BinVal[/b] which do the same for binary. [b]OctStr[/b] and [b]OctVal[/b] do the same for octal.
    [code]
    Unit StrVal ;

    interface

    Procedure BinStr (i : Integer ; Var s : String) ;
    Procedure BinVal (s : String ; Var i : Integer ; Var Code : Integer) ;
    Procedure OctStr (i : Integer ; Var s : String) ;
    Procedure OctVal (s : String ; Var i : Integer ; Var Code : Integer) ;


    implementation

    Procedure BinStr(i : Integer ; Var s : String) ;
    {
    convert an integer to a string in binary format
    }
    begin
    if i < 0 then begin
    i := -i ;
    Str(i, s) ;
    s := '-' + s
    end
    else begin
    s := '' ;
    while i > 0 do begin
    if i MOD 2 > 0 then
    s := '1' + s
    else
    s := '0' + s ;
    i := i DIV 2
    end ;
    if Length (s) = 0 then
    s := '0'
    end ;
    end ;

    Procedure BinVal (s : String ; Var i : Integer ; Var Code : Integer) ;
    {
    convert a string in binary format to an integer
    }
    Var
    Sign, j, n : Integer ;

    begin
    if s[1] IN ['+', '-', '0', '1'] then begin { is 1st char valid? }
    case s[i] of
    '-' : begin
    Sign := -1 ;
    n := 2
    end ;
    '+' : begin
    Sign := 1 ;
    n := 2
    end ;
    else begin
    Sign := 1 ;
    n := 1
    end
    end ; { case }
    i := 0 ;
    for j := n to Length(s) do
    if s[j] IN ['0', '1'] then
    i := 2*i + Ord(s[j]) - Ord('0')
    else begin
    Code := j ;
    Exit
    end ;
    i := Sign * i
    end
    else begin
    i := 0 ;
    Code := 1
    end
    end ;

    Procedure OctStr(i : Integer ; Var s : String) ;
    {
    convert an integer to a string in octal format
    }
    begin
    if i < 0 then begin
    i := -i ;
    Str(i, s) ;
    s := '-' + s
    end
    else begin
    s := '' ;
    while i > 0 do begin
    s := Chr((i MOD 8) + Ord('0')) + s ;
    i := i DIV 8
    end ;
    if Length (s) = 0 then
    s := '0'
    end ;
    end ;

    Procedure OctVal (s : String ; Var i : Integer ; Var Code : Integer) ;
    {
    convert a string in octal format to an integer
    }
    Var
    Sign, j, n : Integer ;

    begin
    if s[1] IN ['+', '-', '0' .. '7'] then begin { is 1st char valid? }
    case s[i] of
    '-' : begin
    Sign := -1 ;
    n := 2
    end ;
    '+' : begin
    Sign := 1 ;
    n := 2
    end ;
    else begin
    Sign := 1 ;
    n := 1
    end
    end ; { case }
    i := 0 ;
    for j := n to Length(s) do
    if s[j] IN ['0' .. '7'] then
    i := 8*i + Ord(s[j]) - Ord('0')
    else begin
    Code := j ;
    Exit
    end ;
    i := Sign * i
    end
    else begin
    i := 0 ;
    Code := 1
    end
    end ;

    end.
    [code]
  • AtexAtex Posts: 268Member
    Binary is stored as a string of bits (0's and 1's) and each represent a value (power of 2).[code]Example:
    Bit number: 7 6 5 4 3 2 1 0
    Binary:[b] 1 0 1 1 0 1 0 1[/b]
    Values:[b] 128 64 32 16 8 4 2 1 [/b]

    Converting to decimal: 1*128 + 0*64 + 1*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1*1 = 171
    (because: 2^7=128, 2^6=64, 2^5=32....)

    Converting a decimal to binary could be done with the aid of the
    bitwise [b]and[/b] or the [b]mod[/b] (modulo) operator, the latter
    being more cpu intensive, therefore slower. Starting from the highest
    bit check for each bit, if set add [b]1[/b] to output else [b]0[/b]
    (e.g. if (value and 64=64) then {<-- bit6 is set} )[/code]Octal numbers are a bit different but the idea is the same...[code]Binary to octal:
    Bit number: 7 6 5 4 3 2 1 0
    Binary:[b] 1 0 1 1 0 1 0 1[/b]
    At his point left pad the bits with [b]0[/b]'s to have the length dividable by 3, adding 0's won't change it's value
    So: 010110101 ( <-- added one 0 )
    Next divide the binary string to pieces of 3, that's why we added 0's to make this possible
    -->[b] 010 110 101[/b]
    Now convert these triplets into their decimal value:
    010 = 0*4 + 1*2 + 0*1 = 2
    110 = 1*4 + 1*2 + 0*1 = 6
    101 = 1*4 + 0*2 + 1*1 = 5
    By concatenating these values results in the octal number: [b]265[/b]
    To convert octal to binary use the same algorithm backwards...
    [/code]
  • ildarildar Posts: 5Member
    :))
    [code]
    {Binary to decimal
    This function gets binary number as a String and will return decimaal number as an Integer}
    function bin2dec(getal:String): Integer;
    var
    I: Integer;
    uitvoer : Integer;
    begin
    uitvoer := 0;
    for I := 0 to length(getal) -1 do
    begin
    if getal[length(getal) - I] = '1' then
    begin
    uitvoer := uitvoer + 1 shl (I);
    end;
    end;
    bin2dec:=uitvoer;
    end;

    {Returns a string in which the character order of a specified string is reversed}
    function ReverseString(S : String): String;
    var
    i : Integer;
    begin
    Result := '';
    For i := Length(S) DownTo 1 Do
    Begin
    Result := Result + Copy(S,i,1) ;
    End;
    End;

    {
    Decimaal to binary
    This function gets a decimal number as Integer and will return the binary number as a String
    }
    function dec2bin(getal:Integer): String;
    var
    uitvoer:String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 2;
    if frac(getal_met_rest) = 0
    then uitvoer := uitvoer + '0'
    else uitvoer := uitvoer + '1';
    getal := trunc(getal_met_rest);
    end;
    dec2bin:=ReverseString(uitvoer);
    end;

    {
    This function gets a decimal number as an Integer and will return the HEX number as a String
    }
    function dec2hex(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    S : Integer;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 16;
    S := getal - (trunc(getal_met_rest)*16);
    if S < 10 then uitvoer := uitvoer + IntToStr(S);
    if S = 10 then uitvoer := uitvoer + 'A';
    if S = 11 then uitvoer := uitvoer + 'B';
    if S = 12 then uitvoer := uitvoer + 'C';
    if S = 13 then uitvoer := uitvoer + 'D';
    if S = 14 then uitvoer := uitvoer + 'E';
    if S = 15 then uitvoer := uitvoer + 'F';
    getal := trunc(getal_met_rest);
    end;
    dec2hex:=ReverseString(uitvoer);
    end;

    {
    This function gets hex number as a String and will return the decimamaal number as an Integer
    }
    function hex2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    dec_number: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    if getal[I] = '1' then dec_number := 1;
    if getal[I] = '2' then dec_number := 2;
    if getal[I] = '3' then dec_number := 3;
    if getal[I] = '4' then dec_number := 4;
    if getal[I] = '5' then dec_number := 5;
    if getal[I] = '6' then dec_number := 6;
    if getal[I] = '7' then dec_number := 7;
    if getal[I] = '8' then dec_number := 8;
    if getal[I] = '9' then dec_number := 9;
    if getal[I] = 'a' then dec_number := 10;
    if getal[I] = 'A' then dec_number := 10;
    if getal[I] = 'b' then dec_number := 11;
    if getal[I] = 'B' then dec_number := 11;
    if getal[I] = 'c' then dec_number := 12;
    if getal[I] = 'C' then dec_number := 12;
    if getal[I] = 'd' then dec_number := 13;
    if getal[I] = 'D' then dec_number := 13;
    if getal[I] = 'e' then dec_number := 14;
    if getal[I] = 'E' then dec_number := 14;
    if getal[I] = 'f' then dec_number := 15;
    if getal[I] = 'F' then dec_number := 15;

    uitvoer := uitvoer + (dec_number * trunc(Power(16, I-1)));
    end;
    hex2dec:=uitvoer;
    end;

    {
    This function gets a decimal number as an Integer and will return the octal number as a String
    }
    function dec2oct(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 8;
    uitvoer := uitvoer + IntToStr(getal - (trunc(getal_met_rest)*8));
    getal := trunc(getal_met_rest);
    end;
    dec2oct:=ReverseString(uitvoer);
    end;

    {
    This function gets octal number as a String and will return the decimamaal number as an Integer
    }
    function oct2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    uitvoer := uitvoer + (StrToInt(getal[I]) * trunc(Power(8, I-1)));
    end;
    oct2dec:=uitvoer;
    end;

    [/code]
  • ildarildar Posts: 5Member
    :))
    [code]
    {Binary to decimal
    This function gets binary number as a String and will return decimaal number as an Integer}
    function bin2dec(getal:String): Integer;
    var
    I: Integer;
    uitvoer : Integer;
    begin
    uitvoer := 0;
    for I := 0 to length(getal) -1 do
    begin
    if getal[length(getal) - I] = '1' then
    begin
    uitvoer := uitvoer + 1 shl (I);
    end;
    end;
    bin2dec:=uitvoer;
    end;

    {Returns a string in which the character order of a specified string is reversed}
    function ReverseString(S : String): String;
    var
    i : Integer;
    begin
    Result := '';
    For i := Length(S) DownTo 1 Do
    Begin
    Result := Result + Copy(S,i,1) ;
    End;
    End;

    {
    Decimaal to binary
    This function gets a decimal number as Integer and will return the binary number as a String
    }
    function dec2bin(getal:Integer): String;
    var
    uitvoer:String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 2;
    if frac(getal_met_rest) = 0
    then uitvoer := uitvoer + '0'
    else uitvoer := uitvoer + '1';
    getal := trunc(getal_met_rest);
    end;
    dec2bin:=ReverseString(uitvoer);
    end;

    {
    This function gets a decimal number as an Integer and will return the HEX number as a String
    }
    function dec2hex(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    S : Integer;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 16;
    S := getal - (trunc(getal_met_rest)*16);
    if S < 10 then uitvoer := uitvoer + IntToStr(S);
    if S = 10 then uitvoer := uitvoer + 'A';
    if S = 11 then uitvoer := uitvoer + 'B';
    if S = 12 then uitvoer := uitvoer + 'C';
    if S = 13 then uitvoer := uitvoer + 'D';
    if S = 14 then uitvoer := uitvoer + 'E';
    if S = 15 then uitvoer := uitvoer + 'F';
    getal := trunc(getal_met_rest);
    end;
    dec2hex:=ReverseString(uitvoer);
    end;

    {
    This function gets hex number as a String and will return the decimamaal number as an Integer
    }
    function hex2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    dec_number: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    if getal[I] = '1' then dec_number := 1;
    if getal[I] = '2' then dec_number := 2;
    if getal[I] = '3' then dec_number := 3;
    if getal[I] = '4' then dec_number := 4;
    if getal[I] = '5' then dec_number := 5;
    if getal[I] = '6' then dec_number := 6;
    if getal[I] = '7' then dec_number := 7;
    if getal[I] = '8' then dec_number := 8;
    if getal[I] = '9' then dec_number := 9;
    if getal[I] = 'a' then dec_number := 10;
    if getal[I] = 'A' then dec_number := 10;
    if getal[I] = 'b' then dec_number := 11;
    if getal[I] = 'B' then dec_number := 11;
    if getal[I] = 'c' then dec_number := 12;
    if getal[I] = 'C' then dec_number := 12;
    if getal[I] = 'd' then dec_number := 13;
    if getal[I] = 'D' then dec_number := 13;
    if getal[I] = 'e' then dec_number := 14;
    if getal[I] = 'E' then dec_number := 14;
    if getal[I] = 'f' then dec_number := 15;
    if getal[I] = 'F' then dec_number := 15;

    uitvoer := uitvoer + (dec_number * trunc(Power(16, I-1)));
    end;
    hex2dec:=uitvoer;
    end;

    {
    This function gets a decimal number as an Integer and will return the octal number as a String
    }
    function dec2oct(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 8;
    uitvoer := uitvoer + IntToStr(getal - (trunc(getal_met_rest)*8));
    getal := trunc(getal_met_rest);
    end;
    dec2oct:=ReverseString(uitvoer);
    end;

    {
    This function gets octal number as a String and will return the decimamaal number as an Integer
    }
    function oct2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    uitvoer := uitvoer + (StrToInt(getal[I]) * trunc(Power(8, I-1)));
    end;
    oct2dec:=uitvoer;
    end;

    [/code]
  • ildarildar Posts: 5Member
    :))
    [code]
    {Binary to decimal
    This function gets binary number as a String and will return decimaal number as an Integer}
    function bin2dec(getal:String): Integer;
    var
    I: Integer;
    uitvoer : Integer;
    begin
    uitvoer := 0;
    for I := 0 to length(getal) -1 do
    begin
    if getal[length(getal) - I] = '1' then
    begin
    uitvoer := uitvoer + 1 shl (I);
    end;
    end;
    bin2dec:=uitvoer;
    end;

    {Returns a string in which the character order of a specified string is reversed}
    function ReverseString(S : String): String;
    var
    i : Integer;
    begin
    Result := '';
    For i := Length(S) DownTo 1 Do
    Begin
    Result := Result + Copy(S,i,1) ;
    End;
    End;

    {
    Decimaal to binary
    This function gets a decimal number as Integer and will return the binary number as a String
    }
    function dec2bin(getal:Integer): String;
    var
    uitvoer:String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 2;
    if frac(getal_met_rest) = 0
    then uitvoer := uitvoer + '0'
    else uitvoer := uitvoer + '1';
    getal := trunc(getal_met_rest);
    end;
    dec2bin:=ReverseString(uitvoer);
    end;

    {
    This function gets a decimal number as an Integer and will return the HEX number as a String
    }
    function dec2hex(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    S : Integer;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 16;
    S := getal - (trunc(getal_met_rest)*16);
    if S < 10 then uitvoer := uitvoer + IntToStr(S);
    if S = 10 then uitvoer := uitvoer + 'A';
    if S = 11 then uitvoer := uitvoer + 'B';
    if S = 12 then uitvoer := uitvoer + 'C';
    if S = 13 then uitvoer := uitvoer + 'D';
    if S = 14 then uitvoer := uitvoer + 'E';
    if S = 15 then uitvoer := uitvoer + 'F';
    getal := trunc(getal_met_rest);
    end;
    dec2hex:=ReverseString(uitvoer);
    end;

    {
    This function gets hex number as a String and will return the decimamaal number as an Integer
    }
    function hex2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    dec_number: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    if getal[I] = '1' then dec_number := 1;
    if getal[I] = '2' then dec_number := 2;
    if getal[I] = '3' then dec_number := 3;
    if getal[I] = '4' then dec_number := 4;
    if getal[I] = '5' then dec_number := 5;
    if getal[I] = '6' then dec_number := 6;
    if getal[I] = '7' then dec_number := 7;
    if getal[I] = '8' then dec_number := 8;
    if getal[I] = '9' then dec_number := 9;
    if getal[I] = 'a' then dec_number := 10;
    if getal[I] = 'A' then dec_number := 10;
    if getal[I] = 'b' then dec_number := 11;
    if getal[I] = 'B' then dec_number := 11;
    if getal[I] = 'c' then dec_number := 12;
    if getal[I] = 'C' then dec_number := 12;
    if getal[I] = 'd' then dec_number := 13;
    if getal[I] = 'D' then dec_number := 13;
    if getal[I] = 'e' then dec_number := 14;
    if getal[I] = 'E' then dec_number := 14;
    if getal[I] = 'f' then dec_number := 15;
    if getal[I] = 'F' then dec_number := 15;

    uitvoer := uitvoer + (dec_number * trunc(Power(16, I-1)));
    end;
    hex2dec:=uitvoer;
    end;

    {
    This function gets a decimal number as an Integer and will return the octal number as a String
    }
    function dec2oct(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 8;
    uitvoer := uitvoer + IntToStr(getal - (trunc(getal_met_rest)*8));
    getal := trunc(getal_met_rest);
    end;
    dec2oct:=ReverseString(uitvoer);
    end;

    {
    This function gets octal number as a String and will return the decimamaal number as an Integer
    }
    function oct2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    uitvoer := uitvoer + (StrToInt(getal[I]) * trunc(Power(8, I-1)));
    end;
    oct2dec:=uitvoer;
    end;

    [/code]
  • ildarildar Posts: 5Member
    :))
    [code]
    {Binary to decimal
    This function gets binary number as a String and will return decimaal number as an Integer}
    function bin2dec(getal:String): Integer;
    var
    I: Integer;
    uitvoer : Integer;
    begin
    uitvoer := 0;
    for I := 0 to length(getal) -1 do
    begin
    if getal[length(getal) - I] = '1' then
    begin
    uitvoer := uitvoer + 1 shl (I);
    end;
    end;
    bin2dec:=uitvoer;
    end;

    {Returns a string in which the character order of a specified string is reversed}
    function ReverseString(S : String): String;
    var
    i : Integer;
    begin
    Result := '';
    For i := Length(S) DownTo 1 Do
    Begin
    Result := Result + Copy(S,i,1) ;
    End;
    End;

    {
    Decimaal to binary
    This function gets a decimal number as Integer and will return the binary number as a String
    }
    function dec2bin(getal:Integer): String;
    var
    uitvoer:String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 2;
    if frac(getal_met_rest) = 0
    then uitvoer := uitvoer + '0'
    else uitvoer := uitvoer + '1';
    getal := trunc(getal_met_rest);
    end;
    dec2bin:=ReverseString(uitvoer);
    end;

    {
    This function gets a decimal number as an Integer and will return the HEX number as a String
    }
    function dec2hex(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    S : Integer;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 16;
    S := getal - (trunc(getal_met_rest)*16);
    if S < 10 then uitvoer := uitvoer + IntToStr(S);
    if S = 10 then uitvoer := uitvoer + 'A';
    if S = 11 then uitvoer := uitvoer + 'B';
    if S = 12 then uitvoer := uitvoer + 'C';
    if S = 13 then uitvoer := uitvoer + 'D';
    if S = 14 then uitvoer := uitvoer + 'E';
    if S = 15 then uitvoer := uitvoer + 'F';
    getal := trunc(getal_met_rest);
    end;
    dec2hex:=ReverseString(uitvoer);
    end;

    {
    This function gets hex number as a String and will return the decimamaal number as an Integer
    }
    function hex2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    dec_number: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    if getal[I] = '1' then dec_number := 1;
    if getal[I] = '2' then dec_number := 2;
    if getal[I] = '3' then dec_number := 3;
    if getal[I] = '4' then dec_number := 4;
    if getal[I] = '5' then dec_number := 5;
    if getal[I] = '6' then dec_number := 6;
    if getal[I] = '7' then dec_number := 7;
    if getal[I] = '8' then dec_number := 8;
    if getal[I] = '9' then dec_number := 9;
    if getal[I] = 'a' then dec_number := 10;
    if getal[I] = 'A' then dec_number := 10;
    if getal[I] = 'b' then dec_number := 11;
    if getal[I] = 'B' then dec_number := 11;
    if getal[I] = 'c' then dec_number := 12;
    if getal[I] = 'C' then dec_number := 12;
    if getal[I] = 'd' then dec_number := 13;
    if getal[I] = 'D' then dec_number := 13;
    if getal[I] = 'e' then dec_number := 14;
    if getal[I] = 'E' then dec_number := 14;
    if getal[I] = 'f' then dec_number := 15;
    if getal[I] = 'F' then dec_number := 15;

    uitvoer := uitvoer + (dec_number * trunc(Power(16, I-1)));
    end;
    hex2dec:=uitvoer;
    end;

    {
    This function gets a decimal number as an Integer and will return the octal number as a String
    }
    function dec2oct(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 8;
    uitvoer := uitvoer + IntToStr(getal - (trunc(getal_met_rest)*8));
    getal := trunc(getal_met_rest);
    end;
    dec2oct:=ReverseString(uitvoer);
    end;

    {
    This function gets octal number as a String and will return the decimamaal number as an Integer
    }
    function oct2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    uitvoer := uitvoer + (StrToInt(getal[I]) * trunc(Power(8, I-1)));
    end;
    oct2dec:=uitvoer;
    end;

    [/code]
  • ildarildar Posts: 5Member
    :))))

    [code]

    {Binary to decimal
    This function gets binary number as a String and will return decimaal namber as an Integer}
    function bin2dec(getal:String): Integer;
    var
    I: Integer;
    uitvoer : Integer;
    begin
    uitvoer := 0;
    for I := 0 to length(getal) -1 do
    begin
    if getal[length(getal) - I] = '1' then
    begin
    uitvoer := uitvoer + 1 shl (I);
    end;
    end;
    bin2dec:=uitvoer;
    end;

    {Returns a string in which the character order of a specified string is reversed}
    function ReverseString(S : String): String;
    var
    i : Integer;
    begin
    Result := '';
    For i := Length(S) DownTo 1 Do
    Begin
    Result := Result + Copy(S,i,1) ;
    End;
    End;

    {
    Decimaal to binary
    This function gets a decimal namber as Integer and will return the binary namber as a String
    }
    function dec2bin(getal:Integer): String;
    var
    uitvoer:String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 2;
    if frac(getal_met_rest) = 0
    then uitvoer := uitvoer + '0'
    else uitvoer := uitvoer + '1';
    getal := trunc(getal_met_rest);
    end;
    dec2bin:=ReverseString(uitvoer);
    end;

    {
    This function gets a decimal number as an Integer and will return the HEX number as a String
    }
    function dec2hex(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    S : Integer;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 16;
    S := getal - (trunc(getal_met_rest)*16);
    if S < 10 then uitvoer := uitvoer + IntToStr(S);
    if S = 10 then uitvoer := uitvoer + 'A';
    if S = 11 then uitvoer := uitvoer + 'B';
    if S = 12 then uitvoer := uitvoer + 'C';
    if S = 13 then uitvoer := uitvoer + 'D';
    if S = 14 then uitvoer := uitvoer + 'E';
    if S = 15 then uitvoer := uitvoer + 'F';
    getal := trunc(getal_met_rest);
    end;
    dec2hex:=ReverseString(uitvoer);
    end;

    {
    This function gets hex number as a String and will return the decimamaal number as an Integer
    }
    function hex2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    dec_number: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    if getal[I] = '1' then dec_number := 1;
    if getal[I] = '2' then dec_number := 2;
    if getal[I] = '3' then dec_number := 3;
    if getal[I] = '4' then dec_number := 4;
    if getal[I] = '5' then dec_number := 5;
    if getal[I] = '6' then dec_number := 6;
    if getal[I] = '7' then dec_number := 7;
    if getal[I] = '8' then dec_number := 8;
    if getal[I] = '9' then dec_number := 9;
    if getal[I] = 'a' then dec_number := 10;
    if getal[I] = 'A' then dec_number := 10;
    if getal[I] = 'b' then dec_number := 11;
    if getal[I] = 'B' then dec_number := 11;
    if getal[I] = 'c' then dec_number := 12;
    if getal[I] = 'C' then dec_number := 12;
    if getal[I] = 'd' then dec_number := 13;
    if getal[I] = 'D' then dec_number := 13;
    if getal[I] = 'e' then dec_number := 14;
    if getal[I] = 'E' then dec_number := 14;
    if getal[I] = 'f' then dec_number := 15;
    if getal[I] = 'F' then dec_number := 15;

    uitvoer := uitvoer + (dec_number * trunc(Power(16, I-1)));
    end;
    hex2dec:=uitvoer;
    end;

    {
    This function gets a decimal number as an Integer and will return the octal number as a String
    }
    function dec2oct(getal:Integer): String;
    var
    uitvoer: String;
    getal_met_rest : Real;
    begin
    while getal > 0 do
    begin
    getal_met_rest := getal / 8;
    uitvoer := uitvoer + IntToStr(getal - (trunc(getal_met_rest)*8));
    getal := trunc(getal_met_rest);
    end;
    dec2oct:=ReverseString(uitvoer);
    end;

    {
    This function gets octal number as a String and will return the decimamaal number as an Integer
    }
    function oct2dec(getal: String) : Integer;
    var
    uitvoer: Integer;
    I: Integer;
    begin
    uitvoer := 0;
    getal := ReverseString(getal);
    for I := 1 to length(getal) do
    begin
    uitvoer := uitvoer + (StrToInt(getal[I]) * trunc(Power(8, I-1)));
    end;
    oct2dec:=uitvoer;
    end;

    [/code]
Sign In or Register to comment.