User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Exams its now or never

  #21  
May 28th, 2008
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.

  1. i, n, foo: integer;
  2. ...
  3. result := 0;
  4. for i := 1 to length( s ) do
  5. begin
  6. // validate digit is a binary digit
  7. if not (s[i] in ['0','1'])
  8. then complain_that_its_not_a_binary_number;
  9. // convert that digit into a number
  10. val( s[i], n, foo );
  11. // power up and add
  12. result := result * 2;
  13. result := result + n;
  14. 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.
Reply With Quote  
Join Date: Dec 2007
Posts: 26
Reputation: manutd4life is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
manutd4life manutd4life is offline Offline
Light Poster

Re: Exams its now or never

  #22  
May 29th, 2008
hello i do it like this:

  1. function bintohex(binary:string):string;
  2. var
  3. counter,upperbound,lowerbound:integer;
  4. tempbin,hex:string;
  5. begin
  6. counter:=length(binary);
  7. upperbound:=0;
  8. lowerbound:=0;
  9. tempbin:='';
  10. hex:='';
  11. while(Upperbound<counter) do
  12. begin
  13. while(lowerbound<=(upperbound+4)) do
  14. begin
  15. tempbin:=tempbin+binary[lowerbound];
  16. lowerbound:=lowerbound+1;
  17. end;
  18.  
  19. if(bintodec(tempbin)=0) then
  20. begin
  21. hex:=hex+'0';
  22. end;
  23. if(bintodec(tempbin)=1) then
  24. begin
  25. hex:=hex+'1';
  26. end;if(bintodec(tempbin)=2) then
  27. begin
  28. hex:=hex+'2';
  29. end;
  30. if(bintodec(tempbin)=3) then
  31. begin
  32. hex:=hex+'3';
  33. end;
  34. if(bintodec(tempbin)=4) then
  35. begin
  36. hex:=hex+'4';
  37. end;
  38. if(bintodec(tempbin)=5) then
  39. begin
  40. hex:=hex+'5';
  41. end;
  42. if(bintodec(tempbin)=6) then
  43. begin
  44. hex:=hex+'6';
  45. end;
  46. if(bintodec(tempbin)=7) then
  47. begin
  48. hex:=hex+'7';
  49. end;
  50. if(bintodec(tempbin)=8) then
  51. begin
  52. hex:=hex+'8';
  53. end;
  54. if(bintodec(tempbin)=9) then
  55. begin
  56. hex:=hex+'9';
  57. end;
  58. if(bintodec(tempbin)=10) then
  59. begin
  60. hex:=hex+'A';
  61. end;
  62. if(bintodec(tempbin)=11) then
  63. begin
  64. hex:=hex+'B';
  65. end;
  66. if(bintodec(tempbin)=12) then
  67. begin
  68. hex:=hex+'C';
  69. end;
  70. if(bintodec(tempbin)=13) then
  71. begin
  72. hex:=hex+'D';
  73. end;
  74. if(bintodec(tempbin)=14) then
  75. begin
  76. hex:=hex+'E';
  77. end;
  78. if(bintodec(tempbin)=15) then
  79. begin
  80. hex:=hex+'F';
  81. end;
  82. Upperbound:=upperbound+4;
  83. tempbin:='';
  84. end;
  85. bintohex:=hex;
  86. 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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Exams its now or never

  #23  
May 29th, 2008
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.
Reply With Quote  
Join Date: Dec 2007
Posts: 26
Reputation: manutd4life is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
manutd4life manutd4life is offline Offline
Light Poster

Re: Exams its now or never

  #24  
May 29th, 2008
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:

  1. Program NumberConversion;
  2.  
  3. Uses wincrt;
  4.  
  5. Const
  6. max = 30; {this is used for declaring ARRAY}
  7.  
  8. Type
  9. hexadecimal = ARRAY[1..max] OF integer;
  10.  
  11. Var
  12. decimal: longint;
  13. i: integer;
  14. hexa: hexadecimal;
  15.  
  16. Procedure Menu; {Procedure to display the Menu.}
  17. Begin
  18. writeln(' Number conversion:'); {This line displayed only {Number conversion) and same for other line.}
  19. writeln(' 1. Binary to decimal');
  20. Writeln(' 2. Octal to decimal');
  21. Writeln(' 3. Binary to hexadecimal');
  22. Writeln(' 4. Exit');
  23. Write(' Enter your choice: ')
  24. End; {Stop the Menu procedure.}
  25.  
  26.  
  27.  
  28. Procedure PressAnyKey; {Procedure to show the Menu by pressing any key when a conversion is finished.}
  29. Begin
  30. Writeln; {leave a line.}
  31. Write('Press Any Key for Main Menu...');
  32. readkey; {Read any key on the keyboard.}
  33. clrscr; {Clear the screen.}
  34. Menu; {Display the Menu.}
  35. End; {Stop the PressAnyKey procedure.}
  36.  
  37.  
  38. function BinToDec(binary:string):integer; {Procedure to convert binary to decimal.}
  39. Var {this line Binary is declared as string.}
  40. Len:integer; {Len means Length declared as integer.}
  41. base:integer; {base is the base of the binary and declared as integer.}
  42. Result:longint; {Result is the final result and declared as integer.}
  43. Begin
  44. Write('Enter a binary number: ');
  45. Readln(binary);
  46. result:=0; {Result initialised as 0.}
  47. base:=1; {base initialised as 1.}
  48. for Len:=length(binary) downto 1 do {loop from string length down to 1 (binary reads from right to left).}
  49. begin
  50. If not (binary[Len] in ['1','0']) then {validation, if the string does not contains 1 , 0 or both.}
  51. begin
  52.  
  53. writeln('This is not a binary number!');
  54. writeln; {leave a line.}
  55. Halt; {Stop the Loop.}
  56.  
  57. end
  58. else
  59. if (binary[len]='1') then {Verify if see 1 in string}
  60. result:= result+ base; {convert Binary to Decimal (The result).}
  61. base:=base*2; {increment base by 2.}
  62. end;
  63. BinToDec:=Result;
  64. End;
  65.  
  66. Procedure OctToDec;
  67. Var
  68. Octal:string;
  69. Len:integer;
  70. counter:integer;
  71. base:integer;
  72. result:longint;
  73. i,code:integer;
  74. Begin
  75. Write('Enter an octal number: ');
  76. readln(Octal);
  77. result:=0;
  78. base:=1;
  79. for Len:=length(octal) downto 1 do {loop from string length down to 1 (Octal reads from right to left).}
  80. Begin
  81. If not (octal[Len] in ['0'..'7']) then {validation, if the string does not contain only from 0 to 7.}
  82. Begin
  83. Writeln('This is not an Octal number!');
  84. Writeln;
  85. Halt; {stop the loop.}
  86.  
  87. end
  88. Else
  89. Val(octal[len],i,code); {This line converts string into integer.}
  90. Result:=result+i*base; {Converts Octal into Decimal (the result).}
  91. base:=base*8; {octal base increment by 8}
  92. end;
  93. Writeln('The decimal number is ',result); {the answer}
  94. end;
  95.  
  96.  
  97. {This procedure works the conversion of dec to hex.}
  98. Procedure convert(decimal: longint; Var i: integer; Var hexa: hexadecimal);
  99. Begin
  100. i := 1;
  101. while decimal <> 0 do
  102. Begin
  103. hexa[i] := decimal mod 16;
  104. decimal := decimal div 16;
  105. i := i + 1;
  106. end;
  107. end;
  108.  
  109. Procedure writetothescreen(i: integer; hexa: hexadecimal);
  110. Var j: integer;
  111. Begin
  112. write('The Hexadecimal is: ');
  113. {This is how you make a hex number}
  114. For j := (i - 1) downto 1 do
  115. Begin
  116. if hexa[j] < 10 then
  117. write(output, hexa[j])
  118. else
  119. Begin
  120. {Here, program checks for number larger than 10 to write a letter instead of number}
  121. Case hexa[j] of
  122. 10: write(output, 'A');
  123. 11: write(output, 'B');
  124. 12: write(output, 'C');
  125. 13: write(output, 'D');
  126. 14: write(output, 'E');
  127. 15: write(output, 'F');
  128. End; {End of Case}
  129. End; {End of ELSE}
  130. End; {End of FOR loop}
  131. End; {End of Procedure}
  132.  
  133.  
  134. Procedure BinToHex; {This contains all the other procedure to get the final answer
  135.   of Binary to hexadecimal.}
  136. var
  137. decimal:longint;
  138. binary:string;
  139. Begin
  140. decimal:=bintodec(binary);
  141. convert(decimal, i, hexa);
  142. writetothescreen(i, hexa);
  143. End;
  144.  
  145.  
  146. Procedure SelectOption;{here this is use to make your choice}
  147. var
  148. option:integer;
  149. binary:string;
  150. Begin
  151. Readln(option);
  152. If not option in [1..4] then
  153. writeln('invalid choice')
  154. Else
  155. If (option=1) Then
  156. begin
  157. clrscr;
  158. Writeln('**Binary to Decimal conversion**');
  159. writeln;
  160. decimal:=BinToDec(binary);
  161. writeln ('The Decimal number is: ',decimal); {Display the Result.}
  162. PressAnyKey;
  163. SelectOption;
  164. end;
  165. If (option=2) then
  166. Begin
  167. clrscr;
  168. Writeln('**Octal to Decimal conversion**');
  169. Writeln;
  170. OctToDec;
  171. PressAnyKey;
  172. SelectOption;
  173. End;
  174. If (option=3) then
  175. Begin
  176. clrscr;
  177. Writeln('**Binary to Hexadecimal conversion**');
  178. writeln;
  179. BinToHex;
  180. writeln;
  181. PressAnyKey;
  182. SelectOption;
  183. End;
  184. If(option=4) then
  185. Begin
  186. clrscr;
  187. Writeln('Press Alt+F4 to exit');
  188. end;
  189. end;
  190.  
  191.  
  192. Begin {The main Program}
  193. Menu;
  194. SelectOption;
  195.  
  196. End.{End the main program}

Thanks Duoas for ur help
thanks very much
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Exams its now or never

  #25  
May 29th, 2008
Good job!

BTW, what compiler are you using? I had to pull out my old TP4 to compile it...
Reply With Quote  
Join Date: Dec 2007
Posts: 26
Reputation: manutd4life is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
manutd4life manutd4life is offline Offline
Light Poster

Re: Exams its now or never

  #26  
May 30th, 2008
you'll get the compiler am using here:

http://www.vetusware.com/download/Tu...%201.5/?id=131

its for windows
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 1:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC