Re: std::string-to-int converter
Posted: Tue Jul 07, 2009 4:43 pm
sure i'll tell you how it works.Ginto8 wrote:I added a char* version.
PiP: How do you do that? teach me!
Do you know about ASCII? (American Standard Code for Information Interchange) it's basically a standard set of codes used to represent sysmbols, commands and characters in computers, C++ uses this standard when it refers to the type char
basically, chars can be any value from 0 to 255 (with it being an 8 bit number), each of these numbers represent a different action and/or character.
in ASCII, codes 48 to 57 represent the digits '0' to '9', what you have to do to convert these characters to digits is to turn the value 48 into 0, 49 into 1, 50 into 2 etc.
you do this by bitmasking the values.
Simply, all this means is to manipulate the binary values so that they become other values.
you do this by using the AND (represented by & in C++) ,OR (| in c++) and NOT (~ in C++) (theres also XOR, but I wont over complicate things)
Basically, what I did with the string was start from the end, read in the first character, check it's range (check whether it is between '0' and '9') then I used a bitmask and ran a bitwise AND operation on it.
The AND operation is very simple, basically you get 2 values, in this case the first could be 48 (which represents '0' in ASCII), you break it down into raw binary values
(the binary sequence below represents the number 48, the leftmost digit represents 128 and the rightmost represents 1, as you move along, you half each time you move along until you reach 1)
0011 0000 = in plain english this means, there are 0 128's, 0 64's , 1 32's and 1 16's 0 8's 0 4's 0 2's and 0 1's
16 + 32 = 48
if you look at the last four bits, you will see they are all zero, which is a pretty neat coincidence, because we need to represent 0, so we need to get rid of all of those 1's in the left most part of the sequence, how do we do that? we run an AND operation!
basically AND makes a comparision between bit fields, if a bit and a corresponding bit on the other number are equal to 1, then the resultant value is one, if either of them equal 0, then the result is 0, simple enough? so only if the first value AND the second value are equal to 1, you get a 1, otherwise, 0
so, we need to get a number that lets 1's 'pass through' on the rightmost side, but 'block' them from coming through on the leftmost side, so the value were going to AND the number with is
0000 1111
as you can see, this makes it impossible to 'let through' the higher bits, so let's see it in action
0011 0000
AND
0000 1111
=
0000 0000 = 0 in decimal
This system can be used to determine digits from 1 to 9, they all end up coming out that way, then we simply multiply the value by a multiple of 10 depending on whether they represent hundreds, tens or units (or thousands or millions!)
you may have noticed I used &0F and not &(meaning AND)0000 1111, this is because I used hex as shorthand, but they mean the same thing, I cant remember off the top of my head the C++ way of telling the compiler you're using binary(it might be 0b followed by a binary sequence), but in hex you use 0x(then the hex value, but I'm sure you probably know that already, if you don't i would be happy to explain that in another post or just link you something)
I explained it as best as I can, if you have any questions, don't hesitate to ask
EDIT: the moral of the story is, a long ass explaination for a short piece of code