•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 401,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,042 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1657 | Replies: 25 | Solved
![]() |
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation:
Rep Power: 11
Solved Threads: 189
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.
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:
Hope this helps.
Just take a single digit at a time. For binary, it should be a zero or a one.
Pascal Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2007
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 1
hello i do it like this:
how u would to it plz today its my last day post plz.
pascal Syntax (Toggle Plain Text)
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.
Last edited by manutd4life : May 29th, 2008 at 11:56 am.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation:
Rep Power: 11
Solved Threads: 189
•
•
Join Date: Dec 2007
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 1
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:
Thanks Duoas for ur help
thanks very much
******************
Number conversion
1. Binary to decimal
2. Octal to decimal
3. Binary to hexadecimal
Enter your choice:_
******************
Here's the code FOR EVERYONE:
pascal Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation:
Rep Power: 11
Solved Threads: 189
•
•
Join Date: Dec 2007
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 1
you'll get the compiler am using here:
http://www.vetusware.com/download/Tu...%201.5/?id=131
its for windows
http://www.vetusware.com/download/Tu...%201.5/?id=131
its for windows
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Word Association Game (Posting Games)
- Exams Registration system (Web Developers' Lounge)
- Strangest excuses students give for missing exams: (Geeks' Lounge)
- certmagic is good for ocp exams (Oracle)
- oracle ocp exams guide ? (Oracle)
- Where to get started with Web Programming (IT Careers and Business)
- Who can help a student with her exams in Java?? (Java)
- XP Testing (Windows NT / 2000 / XP / 2003)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Help me plz File not Found yet file is there
- Next Thread: Fibonaci



Linear Mode