can someone explain exactly how this function works? I know it converts ASCII to
ID: 3757120 • Letter: C
Question
can someone explain exactly how this function works? I know it converts ASCII to integer but for example what does the while loop do. I am new to C and have no idea how they can add 'a' and 'A' to things
int ASCIITOInt (char * string, int base, int sign) I //converts ascii to int int output -0 int i = 1 + sign; while (string[il 1- 11e)t && if (string [i] 'ai string [i] if') { >- e# output = output * base + string [1] -lal + 10; output = output * base + stringlì] - A' + 10; output - output * base + string(i] - 0 etse if (string[1] >= A, && stringli]Explanation / Answer
Actually it can converts binary, octal or hexa decimal into decimal.
we are passing string i.e, string variable, base value(2, 8, 16) and sign into a function.
suppose: string = 1E with converting base is 10.
using while we are iterating each and every character to convert to base 10
initially ouptut = 0
iteration 1:
string[0] is 1 is neither range of 'A' - 'Z' or 'a' - 'z', so it will enter into else case.
output = 0*10+ '1'(in ascii: 49) - '0'(in ascii: 48) (it converts character into digit)
= 0 + 49 - 48 = 0+1 = 1
output = 1
iteration 2:
string[1] is range of 'A' to 'Z', so it came into else if case
output = 1*10 + 'E'(in ascii: 69) - 'A'(in ascii: 65) +10
= 10 + 69 - 65 + 10
= 10+4+10 = 24
output = 24
So final output is 24.
(1E)16 = (24)10
Let me know if you have any clarifications. Thank you...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.