Greetings!

I was looking through this Rabin-Karp algorithm code. Everything is fairly clear to me, except those "-48" numbers (lines 13, 14 and 25)...
So I was just wondering if anyone could explain, why are they used for? (maybe for string to int transformation?). Can you please explain it with example?

Thank you!

Recommended Answers

All 3 Replies

The number 48 is for the character '0' (zero), google for "ascii chart" and you will get a complete list. In the code you posted the -48 is converting a charcter digit '0' to '9' to binary, by subtracting '0' from it.

Ancient Dragon, thanks for your help!
I have looked through ASCII chart...

Still i'm not understanding this conversion quite well. You said '0' to '9' to binary. Can you please give me an example? Let's say for letter 'A', letter 'a' and number 7 (conversion process) - I would be VERY grateful...)

Thanks again!

It only works for numeric digits, anything else would be considered undefined behavior.

char c = '1';
int x = c - '0';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.