- 1

Val is 2 , val is 3,val is 16,val is 15.explain me how it assigns its value

It happens in 1st if case and 3rd else if case..

12th Feb 2021, 9:46 AM
Ajith
Ajith - avatar
4 Antworten
+ 2
What are you talking about? , can you provide more details?
12th Feb 2021, 9:48 AM
Abhay
Abhay - avatar
+ 1
int main() { char hex[17]="25GF"; long long decimal; int i=0,val,len; decimal=0; len=4; len--; for(i=0;hex[i]!='\0';i++) { if(hex[i]>='0' && hex[i]<='9') { val=hex[i]-48; } else if(hex[i]>='a' && hex[i]<='z') { Val=hex[i]-97+10; } else if(hex[i]>='A' && hex[i]<='Z') { Val=hex[i]-65+10; } decimal = decimal + val * pow(17,len); } cout << decimal; } A
12th Feb 2021, 9:55 AM
Ajith
Ajith - avatar
+ 1
for a computer everything is just a boolean, i.e true or false... 1 or 0.... everything comes down to a collection of 1s and 0s.... the same is true with storing characters.... they are stored using a specific convention called ASCII '3'>'2' is nothing but 51>50.... https://theasciicode.com.ar/ check this for full chart of values... now coming to your program.. it's converting a number of base 17 to decimal we take each digit(here a character from the array) then multiply with corresponding power of 17...just like converting a binary number to decimal... the if conditions filter out different clusters of possible values for the digit and then assigned to a variable that at the end is multiplied with power of 17 and added to the result. since the given string can have both lower and upper.... they got different ascii values... hence more conditions... the possible ranges are 1) the character being a digit that is a number between 0 to 9 2) a lower case alphabet between a-z 3) a upper case alphabet between A-Z
12th Feb 2021, 11:27 AM
NaSaPaKri
NaSaPaKri - avatar
0
Tq anyway.i got it
12th Feb 2021, 9:22 PM
Ajith
Ajith - avatar