Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.80 KB | None | 0 0
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   i, number, rest : integer;
  10.   hex : string;
  11.   decision : char;
  12.  
  13. //This function is simple one. It calculates n-th power of p.
  14. function power (p : integer; n : integer) : integer;
  15. begin
  16.    result := 1; //set result to 1 because...
  17.    for i := 1 to n do //...if n is 0 then next instruction is never terminated
  18.    result := result * p  //for n times multiply p
  19. end;
  20.  
  21. function dec2hex (n : integer) : string;
  22. begin
  23.    while (number > 0) do
  24.    begin
  25.     rest := number mod 16; //dzielenie całkowitoliczbowe (result is a whole number)
  26.     number := number div 16; //reszta z dzielenia
  27.     if rest > 10 then Insert(Char((rest-10)+65), result, 0) //becuase result is string and we
  28.     //kind of calculate the number from its and, so we use Insert() to insert calculated digits
  29.     //in front of the whole number
  30.     //Char(x) returns an ASCII charachter with index of x
  31.     //This if is to check if we deal with rest bigger than 10. If yes, we need to change it
  32.     //accordingly to A, B, C and so on. First I subtract 10 to receive index of letter and then
  33.     //I add index of capital A (which is 65) to move to the right index of wanted letter
  34.     else Insert(IntToStr(rest), result, 0);
  35.     //It's simplier if rest is simply number lesser than 10. We just convert it to String (same
  36.     //could be achieved with Char(rest+48), where 48 is ASCII index of 0) and insert it in front of
  37.     //result string.
  38.    end;
  39. end;
  40.  
  41. //That function is most complicated. It took me almost two hours to find that little problem with
  42. //strings and there strange indexing of letters -_-.
  43. //Algorithm is quite simple. We have to read input string from the end (less meaningfull digit)
  44. //convert char type to integer and then add proper power of 16 multiplied by that number.
  45. function hex2dec (s : string) : integer;
  46. begin
  47.     for i := 1 to Length(s) do  //we'll be going from i=1 to the length of s string
  48.     begin
  49.       rest := integer(s[Length(s)-i+1]); //I used "rest" variable to save memory, it's name is irrelevant to function
  50.       //integer() converts anything that is in bracket to integer, in polish it's rzutowanie, I don't know english equivalent
  51.       //s[x] is simply x-th letter of s string, where 1 is first and length(s) is last
  52.       //we'll be going from length(s) to 1, so we are subtracting i with each going through loop to decrement
  53.       //+1 is what I've been looking for, it is to prevent to reach s[0] which gives crazy values
  54.       if rest < 65 then rest := rest - 48 //same thing as in dec2hex, if rest wasn't a letter
  55.       //it's index in ASCII will be greater than 65. If so, we need to subtract 55, otherwise only 48.
  56.       else rest := rest - 55;
  57.       result := result + rest*power(16, i-1);
  58.     end;
  59. end;
  60.  
  61. begin
  62.   hex := '';
  63.   number := 0;
  64.   writeln('1: Dec -> Hex');
  65.   writeln('2: Hex -> Dec');
  66.   write('Choose one of the following options: ');
  67.   readln(decision); //this is to know if we're converting dec -> hex or hex -> dec
  68.   writeln;
  69.   case decision of  //this is to break our program to cases: if user choosed '1' then this, or if user
  70.   //choosed '2' then that
  71.   '1': begin
  72.     write('Please write the number you want to convert: ');
  73.     readln(number); //reads the vaule of number from keyboard
  74.     writeln('This number in hexadecimal system is: ' + dec2hex(number));
  75.     end;
  76.   '2': begin
  77.     write('Please write the number you want to convert: ');
  78.     readln(hex);
  79.     writeln('This number in decimal system is: ' + IntToStr(hex2dec(hex))) //IntToStr is a function
  80.     //that converts Integer type to String, since result of hex2dec() is int, and writeln only accepts
  81.     //strings as arguments
  82.   end;
  83.   else
  84.     writeln('Wrong input.');
  85.   end;
  86.   writeln;
  87.   writeln('Press ENTER to end program');
  88.   readln;
  89. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement