Re: How to convert sequence of characters to numbers
- Posted by useless_ Oct 19, 2012
- 1486 views
Umm, s[1] is '2' , not 2, and '2' = 50, 2 = 2. Likewise s[1..4] = "2012".
I think you get the idea.
useless
Yes, and '0' is 48.
The expression "20121018123010" - '0' is equivalent to {50, 48, 49, 50, 49, 48, 49, 56, 49, 50, 51, 48, 49, 48} - 48 which equals {2, 0, 1, 2, 1, 0, 1, 8, 1, 2, 3, 0, 1, 0}
Now s[1..4] is {2, 0, 1, 2} which multiplied out gives you the year as an integer.
Edit: Corrected conversion error
Wierd, i had to look twice again to see his "s -= '0'" line, but i saw your "- 48" code immeadiately. My mistake, sorry.
EDIT:
I'd like to blame this on the font. The other day, Derek used what looked like a lower case 'o' in some math, and i copy/pasted it to a text editor which displays a greater difference between 0, O, and o. I'd have typed it just as you did, using 48 instead of '0'.
useless