a question on the code of transforming string into double

The following code segment is claimed to be able to transfer the string input into a double output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int chgeNum(string str){	//Ê®Áù½øÖÆת»»Ê®½øÖÆ
	unsigned char ch;
	int len;
	double sum=0,arg;
	len = str.length();
	for(int i=0;i<len;i++){
		ch = str[i];
		arg = ch;
		if(47<arg&&arg<58){
			arg = (arg-48)*pow(16.0,(len-1-i));
		}else{
			arg = (arg-55)*pow(16.0,(len-1-i));
		}
		sum = arg + sum;
	}
	return (int)sum;
}


I do not understand what does the if-else do here
1
2
3
4
5
if(47<arg&&arg<58){
			arg = (arg-48)*pow(16.0,(len-1-i));
		}else{
			arg = (arg-55)*pow(16.0,(len-1-i));
		}


And why return (int)sum; not return (double)sum here?
If anyone claims this works with doubles, he's prolly lying. It's definitely returning an integer here. It looks like a hexadecimal converter to me. Though I don't get what the part in else does, the part in if extracts the number from the ascii digit.

EDIT: definitely Hex string to int converter. The part in if is for digits, the part in else for the lower case letters from a-f.
Last edited on
well, the ascii value of the character '0' is 48 and of '9' is 57, so the if-part is converting ascii x to decimal x.
Topic archived. No new replies allowed.