Jump to content
• Advertisement

#### Archived

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

# Question relating to C++

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement
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 this post

##### 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 this post

##### 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;

#### Share this post

##### Share on other sites
Huge, not Hugh. Unless your name is Hugh.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632977
• Total Posts
3009674
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!