Welcome to Dream.In.Code
Getting Help is Easy!

Join 119,726 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,436 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



URGENT help PASCAL

 
Reply to this topicStart new topic

URGENT help PASCAL, binary to decimal

manutd4life
post 24 May, 2008 - 10:55 AM
Post #1


New D.I.C Head

*
Joined: 24 May, 2008
Posts: 3

i wrote my codes but my prob is:
1. when u read a binary it start from the right, but 4 ME IT START FROM THE LEFT
EXAMPLE: 110 = 6
FOR ME THE ANS IS 3
SO HELP PLZZ
2. a code to validate
if some1 enter 1002 as binary disply error
hope i got help

here's my code plz add the validation code and correct the first prob

CODE
program BinToDec;

uses wincrt;

Var
   Binary:string;
   Len:integer;
   base:integer;
   counter:integer;
   Result:integer;

Begin
     writeln('Enter a binary number: ');
     Readln(Binary);
     Len:= Length(binary);
     Base:=1;
     Result:=0;
     For counter:=1 to Len do
         begin;
               if (Binary[counter]='1') then
                  result:= result+ base;
                  base:=base*2;
         end;

     writeln(result);
end.
User is offlineProfile CardPM

Go to the top of the page


Ambercroft
post 24 May, 2008 - 08:19 PM
Post #2


D.I.C Head

Group Icon
Joined: 5 Jan, 2007
Posts: 97



Thanked 2 times

Dream Kudos: 25
My Contributions


Reverse your loop. Start from 'len' and decrement to 1. That will solve your input sequence.

Then check your input for a 1 ( which you have done ) or a 0. If neither is input then produce your error output and exit.

If, elseif ,else type structure should work well here.
User is offlineProfile CardPM

Go to the top of the page

manutd4life
post 25 May, 2008 - 02:47 AM
Post #3


New D.I.C Head

*
Joined: 24 May, 2008
Posts: 3

Some1 Help me on how will i modify this code for octal to decimal
Sorry i forget to thank u
THANKS YOU and help again
User is offlineProfile CardPM

Go to the top of the page

Ambercroft
post 25 May, 2008 - 07:31 PM
Post #4


D.I.C Head

Group Icon
Joined: 5 Jan, 2007
Posts: 97



Thanked 2 times

Dream Kudos: 25
My Contributions


With Octal you are now working with base eight. So now you will have a choice of 8 values not 2. So check that input is 0 - 7

Given 234;

read first character (2) add to total, if this is not the last char multiply total by 8
read next character (3) add to total, if this is not the last char multiply total by 8
etc

If you want it together with your last prgm, look for O as the first character and use the appropriate conversion. Hex too? cool.gif
User is offlineProfile CardPM

Go to the top of the page

manutd4life
post 27 May, 2008 - 10:48 AM
Post #5


New D.I.C Head

*
Joined: 24 May, 2008
Posts: 3

can any1 give me a full explaination on how to convert binary to hexadecimal.
plz help

thanks in advance
User is offlineProfile CardPM

Go to the top of the page

Ambercroft
post 27 May, 2008 - 05:23 PM
Post #6


D.I.C Head

Group Icon
Joined: 5 Jan, 2007
Posts: 97



Thanked 2 times

Dream Kudos: 25
My Contributions


Each group of four binary digits will convert to a hex character.
So convert each group to a decimal number 0 - 15.
0 to 9 will be the same as decimal but the last 6 will convert to A to F.

Write some code and see what you can do. cool.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/15/08 04:02PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month