Hi am in prob. 4 me its now or never to pass. get a program to do.
Convert binary to decimal
octal to decimal
binary to hexadecimal
some1 can help me.
i need some PRO so if interest post ur mail id and i'll mail u my paper
if some1 no some these codes so post plz WITH COMMENTS
plz reply and help me.

We won't do your homework or reply offline.

However, I can help you think through your problem.

A number is a number is a number is a number, no matter how it is represented for us humans. 10 dec == A hex == 12 oct == 1010 bin. The only difference is in the radix.

Common radices:
dec: base 10
hex: base 16
oct: base 8
bin: base 2

The radix is the difference between digit powers. In decimal:
1 == 1 * 10^0
10 == 1 * 10^1
100 == 1 * 10^2
1,000 == 1 * 10^3
etc.

The same is true in other radices. For example, binary:
1 == 2 * 2^0 (001b == 1 * 10b^0)
2 == 2 * 2^1 (010b == 1 * 10b^1)
4 == 2 * 2^2 (100b == 1 * 10b^2)
etc.

So, to convert between bases, you first need to decompose a human-readable representation from its radix into an actual number. Then you need to compose that number into the desired human-readable representation of a given radix.

For example: convert 1010 binary into a number:

str    := '1010';
radix  := 2;
number := (char_to_int( str[1] ) * pow( radix, 3 ))
        + (char_to_int( str[2] ) * pow( radix, 2 ))
        + (char_to_int( str[3] ) * pow( radix, 1 ))
        + (char_to_int( str[4] ) * pow( radix, 0 ));

Neither 'char_to_int' nor 'pow' exist, you'll have to write them. But you should be able to see the progression for each power.

Now, for the basic algorithm:

number := 0;
for i := 1 to length( str )
  number := (number * radix) +char_to_int( str[i] );

To convert a number into a string is the reverse process.

Hope this helps.

i'll make this and its work well convert binary 2 decimal comment the first line and the others plz

Program BinToDec (input,output);
uses wincrt;
Var
   strBin: string;
   intLen: integer;
   intCol: integer;
   intDec: integer;
   intLoop: integer;
   strDigit: string;

begin;
      Write('Enter a binary number: ');
      Readln(strBin);
      intLen:=Length(strBin);
      intCol:=1;
      intDec:=0;
      For intLoop:= 1 to intLen do
          begin;
          strDigit:=copy(strBin,intLen,1);
          if strDigit = '1' then
             intDec:= intDec + intCol;
          intCol:= intCol * 2;
          end;
      writeln(strBin,' is ',intDec, ' as a decimal number.');
      readln;
End.

i got some help in my exercise book so can u plz comments this
and help me how can i change this code to convert octal to decimal and binary to hexadecimal

HELP radix is the base or explain;
and thanks for ur help

You are very close.

You need to move line 22 up before line 19.
On line 19 you accidentally used the wrong variable. (Also, is the same as strDigit := strBin[[b][i]intLoop[/i][/b]] .)

The difference between binary (radix 2) and some other system is how much you multiply by on line 19.

can u help me plese for OCTAL to decimal and Binary to hexadecimal.
its for 30 may 2008
help how can i validate (mean if some one put 1001a its not good)
help plz

I've already given you all the help I can without giving you the answer. You have to use your brain now.

Think about what you know about converting binary to decimal, and apply the same logic to octal. Then try it and see if it works.

I suggest you write yourself a little function that takes a character and turns it into an integer or complains if it is invalid for the base. Its header might look something like this: [B]function[/B] CharToInt( c: [B]char[/B]; base: [B]integer[/B] ): [B]integer[/B]; Then you can say intCol := CharToInt( 'A', 8 ); If A is valid for octal (base 8) then intCol gets the value 10.
If A is not valid for octal then intCol gets the value -1 (invalid).
Etc.

Good luck.

Got problem when converting Octal to decimal
Help plz
Here's the code:

Procedure OctToDec;
Var
   Octal:string;
   Len:integer;
   counter:integer;
   base:integer;
   result:integer;
Begin
     Write('Enter an octal number: ');
     readln(Octal);
     Counter:=1;
     result:=0;
     base:=1;
     for Len:=length(octal) downto counter do
         Begin
              If not (octal[Len] in ['0'..'7']) then
                 Begin
                      Writeln('This is not an Octal number!');
                      Halt;

                 end
              Else
                  Result:=result+octal[len]*base;
                  base:=base*8;
         end;
     Writeln(result);
end;

thanks for the help

Remember, a char is a different type than an integer. You cannot add or multiply characters.

Also remember, the point is that the digits 0, 1, 2, 3, ... are representations of numbers, not numbers themselves.

Line 23: Result:=result+[b]octal[len][/b]*base; You'll have to convert the bolded part from a character into a number. I recommend writing yourself a little procedure to do it, so you can change the code to something like: Result:=result+[b]DigitToInt(octal[len])[/b]*base; Very good job on the procedure, by the way.

You don't actually need the 'counter' variable. Just use [b]downto[/b] 1 .

Hope this helps.

Thank you Very much

so i remain only the Binary to Hexadecimal to do
how can i modify the code for this, its a bit harder 4 me to understand

Thank you again

Result:=result+DigitToInt(octal[len])*base;

unknown identifier

i use

Var
   i,code:integer;
 {4 line 23}
   Val(octal[len],i,code); 
    Result:=result+i*base;

I recommend writing yourself a little procedure to do it

You'll have to write that function yourself.

For hexadecimal, remember that the values of the digits are:
digits: values
0..9: 0..9
A..F: 10..15

So the DigitToInt( 'C' ) should return the value 12.
Good luck.

i mean
4 Hexadecimal dont no how to break it in 4
e.g: 10110011101

will become start from right 0101 1001 1101
This is 5 9 D

Answer is 59D

10110011101 is binary. You don't have to break it into anything. Just convert it to a number.

Once you get your number from the first step, convert it into hexadecimal: 59D.

Remember, the difference between digit powers is the radix.
Remember, binary and decimal and octal and whatever is text, writing, strings, human readable stuff. They aren't numbers.

Convert a string to a number using one radix.
Convert the number to a string using another radix.

Just convert it 4 a number YOU mean Convert bin to dec
and then convert the Dec into Hex

is wat i understand is right?

No, I mean convert bin (a string) to integer. Then convert the integer to hex (another string).

I hope that makes more sense.

am stressing submit date is 30 may 2008 i have to make bin to hex else i'll lose 20 marks.
plz more explanation on how to convert this integer to hex. Binary (string) to integer i no how to do this, but then i have no idea. plz more explanation.

You've already got code that converts octal to a number (which you call OctToDec).

Remember, "octal", "decimal", etc. --these are not numbers. They are strings. Only people read and write strings.

A number is an integer or a real or some other scalar type. Computers understand numbers, not strings.

So conversion from, say, binary to octal is a two-step process.

1. Convert your binary (string) in to a number (integer).
2. Convert your number (integer) to an octal (string).

You'll need two procedures: one for each step.

Remember, you've already done step 1 once (with an octal string to an integer). You can do it the same way for binary or decimal or hexadecimal --the only difference between them is the radix: 2 (binary), 8 (octal), 10 (decimal), or 16 (hexadecimal).
For your OctToDec routine (listed here) you've got the radix (the number 8) on line 24, and the set of valid digits on line 16. I don't know how you fixed line 23 to convert the digit (a character) to a number (integer), but you'll have to do the same sort of thing for every radix. Except for those three lines, everything else is the same.

To go the other way the math is just reversed. Instead of building up a number by tearing down a string, you'll build up a string by tearing down a number.

Think about it a bit and give it a go. I'll check back to see how you're doing.

i solve the line 23 by using

var
     i:integer;
     code:integer;

Begin  
  
     Val(octal[len],i,code);
     Result:=Result+i*base;

my problem is how to make this


0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111

this break in 4 1010 = A

Forget the 4 digits thing. You are trying to skip a step. (Though you may not realize it...)

Just take a single digit at a time. For binary, it should be a zero or a one.

i, n, foo: integer;
...
result := 0;
for i := 1 to length( s ) do
  begin
  // validate digit is a binary digit
  if not (s[i] in ['0','1'])
    then complain_that_its_not_a_binary_number;
  // convert that digit into a number
  val( s[i], n, foo );
  // power up and add
  result := result * 2;
  result := result + n;
  end;

This is the exact same thing you did before with the octal: only three things have changed:
line 7: the set of valid digits have changed
line 10: ok, well that hasn't changed...
line 12: the radix is 2 instead of 8

You'll have to watch how you translate individual digits from hexadecimal. I suggest using upcase, pos, and a string that looks like this: '0123456789ABCDEF' Hope this helps.

hello i do it like this:

function bintohex(binary:string):string;
var
counter,upperbound,lowerbound:integer;
tempbin,hex:string;
begin
  counter:=length(binary);
  upperbound:=0;
  lowerbound:=0;
  tempbin:='';
  hex:='';
      while(Upperbound<counter) do
        begin
           while(lowerbound<=(upperbound+4)) do
             begin
                 tempbin:=tempbin+binary[lowerbound];
                 lowerbound:=lowerbound+1;
             end;

            if(bintodec(tempbin)=0) then
                       begin
                         hex:=hex+'0';
                       end;
                       if(bintodec(tempbin)=1) then
                       begin
                         hex:=hex+'1';
                       end;if(bintodec(tempbin)=2) then
                       begin
                         hex:=hex+'2';
                       end;
                       if(bintodec(tempbin)=3) then
                       begin
                         hex:=hex+'3';
                       end;
                       if(bintodec(tempbin)=4) then
                       begin
                         hex:=hex+'4';
                       end;
                       if(bintodec(tempbin)=5) then
                       begin
                         hex:=hex+'5';
                       end;
                       if(bintodec(tempbin)=6) then
                       begin
                         hex:=hex+'6';
                       end;
                       if(bintodec(tempbin)=7) then
                       begin
                         hex:=hex+'7';
                       end;
                       if(bintodec(tempbin)=8) then
                       begin
                         hex:=hex+'8';
                       end;
                       if(bintodec(tempbin)=9) then
                       begin
                         hex:=hex+'9';
                       end;
                      if(bintodec(tempbin)=10) then
                       begin
                         hex:=hex+'A';
                       end;
                       if(bintodec(tempbin)=11) then
                       begin
                         hex:=hex+'B';
                       end;
                       if(bintodec(tempbin)=12) then
                       begin
                         hex:=hex+'C';
                       end;
                       if(bintodec(tempbin)=13) then
                       begin
                         hex:=hex+'D';
                       end;
                       if(bintodec(tempbin)=14) then
                       begin
                         hex:=hex+'E';
                       end;
                       if(bintodec(tempbin)=15) then
                       begin
                         hex:=hex+'F';
                       end;
             Upperbound:=upperbound+4;
             tempbin:='';
        end;
         bintohex:=hex;
end;

how u would to it plz today its my last day post plz.

I'm sorry, but I've already given you the answer without actually writing code. You'll have to think about it yourself now...

Good luck.

Program finished

******************
Number conversion
1. Binary to decimal
2. Octal to decimal
3. Binary to hexadecimal
Enter your choice:_

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

Here's the code FOR EVERYONE:

Program NumberConversion;

Uses wincrt;

Const
   max = 30; {this is used for declaring ARRAY}

Type
   hexadecimal = ARRAY[1..max] OF integer;

Var
   decimal: longint;
   i: integer;
   hexa: hexadecimal;

Procedure Menu; {Procedure to display the Menu.}
Begin
     writeln(' Number conversion:'); {This line displayed only {Number conversion) and same for other line.}
     writeln('   1. Binary to decimal');
     Writeln('   2. Octal to decimal');
     Writeln('   3. Binary to hexadecimal');
     Writeln('   4. Exit');
     Write(' Enter your choice: ')
End; {Stop the Menu procedure.}



Procedure PressAnyKey; {Procedure to show the Menu by pressing any key when a conversion is finished.}
Begin
     Writeln; {leave a line.}
     Write('Press Any Key for Main Menu...');
     readkey; {Read any key on the keyboard.}
     clrscr; {Clear the screen.}
     Menu; {Display the Menu.}
End; {Stop the PressAnyKey procedure.}


function BinToDec(binary:string):integer; {Procedure to convert binary to decimal.}
Var  {this line Binary is declared as string.}
   Len:integer; {Len means Length declared as integer.}
   base:integer; {base is the base of the binary and declared as integer.}
   Result:longint; {Result is the final result and declared as integer.}
Begin
     Write('Enter a binary number: ');
     Readln(binary);
     result:=0; {Result initialised as 0.}
     base:=1; {base initialised as 1.}
     for Len:=length(binary) downto 1 do {loop from  string length down to 1 (binary reads from right to left).}
         begin                                                                                                                
               If not (binary[Len] in ['1','0']) then {validation, if the string does not contains 1 , 0 or both.}
                  begin           

                        writeln('This is not a binary number!');
                        writeln; {leave a line.}
                        Halt; {Stop the Loop.}
                        
                  end
               else
                   if (binary[len]='1') then {Verify if see 1 in string}
                      result:= result+ base; {convert Binary to Decimal (The result).}
                      base:=base*2; {increment base by 2.}            
         end;
    BinToDec:=Result;
End;

Procedure OctToDec;
Var
   Octal:string;
   Len:integer;
   counter:integer;
   base:integer;
   result:longint;
   i,code:integer;
Begin
     Write('Enter an octal number: ');
     readln(Octal);
     result:=0;
     base:=1;
     for Len:=length(octal) downto 1 do {loop from  string length down to 1 (Octal reads from right to left).}
         Begin
              If not (octal[Len] in ['0'..'7']) then {validation, if the string does not contain only from 0 to 7.}
                 Begin
                      Writeln('This is not an Octal number!');
                      Writeln;
                      Halt; {stop the loop.}

                 end
              Else
                  Val(octal[len],i,code); {This line converts string into integer.}
                  Result:=result+i*base; {Converts Octal into Decimal (the result).}
                  base:=base*8; {octal base increment by 8}
         end;
     Writeln('The decimal number is ',result); {the answer}
end;


      {This procedure works the conversion of dec to hex.}
Procedure convert(decimal: longint; Var i: integer; Var hexa: hexadecimal);
Begin
   i := 1;
   while decimal <> 0 do
         Begin
              hexa[i] := decimal mod 16;
              decimal := decimal div 16;
              i := i + 1;
         end;
end;

Procedure writetothescreen(i: integer; hexa: hexadecimal);
Var j: integer;
Begin
   write('The Hexadecimal is: ');
   {This is how you make a hex number}
      For j := (i - 1) downto 1 do
          Begin
               if hexa[j] < 10 then
            write(output, hexa[j])
               else
                   Begin
{Here, program checks for number larger than 10 to write a letter instead of number}
            Case hexa[j] of
               10: write(output, 'A');
               11: write(output, 'B');
               12: write(output, 'C');
               13: write(output, 'D');
               14: write(output, 'E');
               15: write(output, 'F');
            End; {End of Case}
         End; {End of ELSE}
   End; {End of FOR loop}
End; {End of Procedure}


Procedure BinToHex; {This contains all the other procedure to get the final answer
                    of Binary to hexadecimal.}
var
   decimal:longint;
   binary:string;
Begin
   decimal:=bintodec(binary);
   convert(decimal, i, hexa);
   writetothescreen(i, hexa);
End;


Procedure SelectOption;{here this is use to make your choice}
var
   option:integer;
   binary:string;
Begin
     Readln(option);
     If not option in [1..4] then
        writeln('invalid choice')
     Else
         If (option=1) Then
            begin
                 clrscr;
                 Writeln('**Binary to Decimal conversion**');
                 writeln;
                 decimal:=BinToDec(binary);
                 writeln ('The Decimal number is: ',decimal); {Display the Result.}
                 PressAnyKey;
                 SelectOption;
            end;
         If (option=2) then
            Begin
                 clrscr;
                 Writeln('**Octal to Decimal conversion**');
                 Writeln;
                 OctToDec;
                 PressAnyKey;
                 SelectOption;
            End;
         If (option=3) then
            Begin
                 clrscr;
                 Writeln('**Binary to Hexadecimal conversion**');
                 writeln;
                 BinToHex;
                 writeln;
                 PressAnyKey;
                 SelectOption;
            End;
         If(option=4) then
            Begin
                 clrscr;
                 Writeln('Press Alt+F4 to exit');
            end;
end;


Begin {The main Program}
     Menu;
     SelectOption;
     
End.{End the main program}

Thanks Duoas for ur help
thanks very much

This question has already been answered. Start a new discussion instead.