Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project2;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- var
- i, number, rest : integer;
- hex : string;
- decision : char;
- //This function is simple one. It calculates n-th power of p.
- function power (p : integer; n : integer) : integer;
- begin
- result := 1; //set result to 1 because...
- for i := 1 to n do //...if n is 0 then next instruction is never terminated
- result := result * p //for n times multiply p
- end;
- function dec2hex (n : integer) : string;
- begin
- while (number > 0) do
- begin
- rest := number mod 16; //dzielenie całkowitoliczbowe (result is a whole number)
- number := number div 16; //reszta z dzielenia
- if rest > 10 then Insert(Char((rest-10)+65), result, 0) //becuase result is string and we
- //kind of calculate the number from its and, so we use Insert() to insert calculated digits
- //in front of the whole number
- //Char(x) returns an ASCII charachter with index of x
- //This if is to check if we deal with rest bigger than 10. If yes, we need to change it
- //accordingly to A, B, C and so on. First I subtract 10 to receive index of letter and then
- //I add index of capital A (which is 65) to move to the right index of wanted letter
- else Insert(IntToStr(rest), result, 0);
- //It's simplier if rest is simply number lesser than 10. We just convert it to String (same
- //could be achieved with Char(rest+48), where 48 is ASCII index of 0) and insert it in front of
- //result string.
- end;
- end;
- //That function is most complicated. It took me almost two hours to find that little problem with
- //strings and there strange indexing of letters -_-.
- //Algorithm is quite simple. We have to read input string from the end (less meaningfull digit)
- //convert char type to integer and then add proper power of 16 multiplied by that number.
- function hex2dec (s : string) : integer;
- begin
- for i := 1 to Length(s) do //we'll be going from i=1 to the length of s string
- begin
- rest := integer(s[Length(s)-i+1]); //I used "rest" variable to save memory, it's name is irrelevant to function
- //integer() converts anything that is in bracket to integer, in polish it's rzutowanie, I don't know english equivalent
- //s[x] is simply x-th letter of s string, where 1 is first and length(s) is last
- //we'll be going from length(s) to 1, so we are subtracting i with each going through loop to decrement
- //+1 is what I've been looking for, it is to prevent to reach s[0] which gives crazy values
- if rest < 65 then rest := rest - 48 //same thing as in dec2hex, if rest wasn't a letter
- //it's index in ASCII will be greater than 65. If so, we need to subtract 55, otherwise only 48.
- else rest := rest - 55;
- result := result + rest*power(16, i-1);
- end;
- end;
- begin
- hex := '';
- number := 0;
- writeln('1: Dec -> Hex');
- writeln('2: Hex -> Dec');
- write('Choose one of the following options: ');
- readln(decision); //this is to know if we're converting dec -> hex or hex -> dec
- writeln;
- case decision of //this is to break our program to cases: if user choosed '1' then this, or if user
- //choosed '2' then that
- '1': begin
- write('Please write the number you want to convert: ');
- readln(number); //reads the vaule of number from keyboard
- writeln('This number in hexadecimal system is: ' + dec2hex(number));
- end;
- '2': begin
- write('Please write the number you want to convert: ');
- readln(hex);
- writeln('This number in decimal system is: ' + IntToStr(hex2dec(hex))) //IntToStr is a function
- //that converts Integer type to String, since result of hex2dec() is int, and writeln only accepts
- //strings as arguments
- end;
- else
- writeln('Wrong input.');
- end;
- writeln;
- writeln('Press ENTER to end program');
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement