- 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..
4 Antworten
+ 2
What are you talking about? , can you provide more details?
+ 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
+ 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
0
Tq anyway.i got it