• Popular Now

• 13
• 15
• 27
• 9
• 9

Archived

This topic is now archived and is closed to further replies.

Question relating to C++

This topic is 5679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Just wondering, why I have to postfix the " -''0'' " at the back of the expression?

//my constructor implementation

HughInt::HughInt(const char *str)
{
for(int x=0;x<30;x++)
integer[x] = 0;

for(int count = 30 - strlen(str), int i=0;count <30;count++,i++)
{
if(isdigit(str[i]))
integer[count] = str[i]-''0''; // <- why needed?

}
}

data members in class definition: short integer[30]; The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant

Share on other sites
Because you want to translate the digit (That is in ascii) to an integer.
So if the character is ''0'', ''0'' - ''0'' will give you 0
And ''2'' - ''0'' will give you 2, Regardless of the ASCII value of ''0''

Goblineye Entertainment
The road to success is always under construction

Share on other sites
it is a way of getting the numeric value from the ascii value:

char ASCII
''0'' 48
''1'' 49
''2'' 50
''3'' 51
''4'' 52
''5'' 53
''6'' 54
''7'' 55
''8'' 56
''9'' 57

that code seems to translate a string representation of a number into the actual number (i.e. "12345" -> 12345).

I see

Share on other sites
Notice that ''0'' is 48, which is two set bits (32+16).
So you can convert from int to ascii with a bit mask.
char x = ''4'';
int i = x & ~0x30;
char c = i | 0x30;