Page 2 of 3

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 4:43 pm
by programmerinprogress
Ginto8 wrote:I added a char* version.

PiP: How do you do that? teach me! ;)
sure i'll tell you how it works.

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 :lol:

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 5:21 pm
by Ginto8
well, I knew most of that; just was wondering why you were ANDing it by 15 (0x0F). :lol: But either way your post is going to be a pretty good reference for those who don't know. ;) Thanks!

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 5:27 pm
by programmerinprogress
Ginto8 wrote:well, I knew most of that; just was wondering why you were ANDing it by 15 (0x0F). :lol: But either way your post is going to be a pretty good reference for those who don't know. ;) Thanks!
I wasn't sure of your experiene level, I know you can use SDL and stuff, but afterall, I think you said you were 12/13, and to be honest, I couldn't of wrapped my head around that when I was 12/13 :lol:

yeah, the ANDing by 15 just reduces the number to a single digit number, sorted ;)

PS: your method actually is a lot easier to decode than mine, they both mean the same thing really, I just take away my numbers differently!

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 5:32 pm
by Joeyotrevor
Actually bitwise NOT is ~, ! is a boolean NOT.

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 5:36 pm
by Netwatcher
Neat little function ;)

Isn't that 0 to 255 for unsigned
and -128 to 127 signed? (last bit is used for sign convention)

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 6:38 pm
by Ginto8
Netwatcher wrote:Neat little function ;)

Isn't that 0 to 255 for unsigned
and -128 to 127 signed? (last bit is used for sign convention)
Yes
:lol:
programmerinprogress wrote:
Ginto8 wrote:well, I knew most of that; just was wondering why you were ANDing it by 15 (0x0F). :lol: But either way your post is going to be a pretty good reference for those who don't know. ;) Thanks!
I wasn't sure of your experiene level, I know you can use SDL and stuff, but afterall, I think you said you were 12/13, and to be honest, I couldn't of wrapped my head around that when I was 12/13 :lol:

yeah, the ANDing by 15 just reduces the number to a single digit number, sorted ;)

PS: your method actually is a lot easier to decode than mine, they both mean the same thing really, I just take away my numbers differently!
Yes, I'm 12 (13 in the fall), but I have found that I have an almost natural understanding of how computers store data. Luckily, this means that I learn things like bitshifting and bit manipulations without too much difficulty. Still trying to get my head around HOW computers store floating point numbers, but I'm working on it. ;)

And I use OpenGL now, not SDL =P, but the jist is understood.

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 6:42 pm
by programmerinprogress
Joeyotrevor wrote:Actually bitwise NOT is ~, ! is a boolean NOT.
yeah, you're right, I shall make the change promptly, I haven't used bitwise NOT for a while

and, as for signed and unsigned binary numbers, don't get me started, i'm still traumatised my my exam where we had to work out floating point (I think it was signed too, but I honestly can't remember :lol: )

it was all convert this to that, and back again lol

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 6:45 pm
by Ginto8
programmerinprogress wrote:
Joeyotrevor wrote:Actually bitwise NOT is ~, ! is a boolean NOT.
yeah, you're right, I shall make the change promptly, I haven't used bitwise NOT for a while

and, as for signed and unsigned binary numbers, don't get me started, i'm still traumatised my my exam where we had to work out floating point (I think it was signed too, but I honestly can't remember :lol: )

it was all convert this to that, and back again lol
Signed is usually 2's complement -- NOT the bits and add 1. So it isn't too hard to encode/decode

Re: std::string-to-int converter

Posted: Tue Jul 07, 2009 8:04 pm
by Netwatcher
Ginto8 wrote: Yes, I'm 12 (13 in the fall), but I have found that I have an almost natural understanding of how computers store data. Luckily, this means that I learn things like bitshifting and bit manipulations without too much difficulty. Still trying to get my head around HOW computers store floating point numbers, but I'm working on it. ;)

And I use OpenGL now, not SDL =P, but the jist is understood.
Yea, I know what you mean, since I was young I had a talent for fixing stuff and a passion for problem solving, I was so excited about trying new stuff... that lead me to CG art.

now I'm 17... wasted almost 9 years of my life dealing with graphics until the beginning of this year,
when I found out about programming(well, the first thing I made was a chat spammer with visual basic but eh...).
EE & CS Stanford lectures on youtube FTW! ( :lol: )

I also think I saw you in another forum, are you in bytesIT?

Re: std::string-to-int converter

Posted: Wed Jul 08, 2009 4:51 am
by zodiac976
Lucky, I spent most of my teen years doing much of nothing and
it took me about 14+ years to come across something I really
enjoy which is programming/game programming ;P. I got lucky
though I just came across a C++ class in college and that's how I
got hooked. I am way behind everyone though :(.

Re: std::string-to-int converter

Posted: Wed Jul 08, 2009 5:07 am
by programmerinprogress
Ginto8 wrote: Still trying to get my head around HOW computers store floating point numbers, but I'm working on it. ;)
I've got two words for you that should help, exponent and mantissa ;)

I CAN work out floating point numbers, I mean I did it in my exam and got an A, but I can't actually remember how you do it anymore, but i'll definately find out again, it used to explain it in my computer sicence book ,unfortunately that was on loan from the college, and good general computing books are hard to come by (at A-Level anyway) because they just changed the way they teach it! (I was on the old spec)

I'll probably get a few good computer science books (well I guess I have to lol) when I go to uni.

Re: std::string-to-int converter

Posted: Wed Jul 08, 2009 5:19 am
by Netwatcher
Ginto8 wrote:
programmerinprogress wrote:
Joeyotrevor wrote:Actually bitwise NOT is ~, ! is a boolean NOT.
yeah, you're right, I shall make the change promptly, I haven't used bitwise NOT for a while

and, as for signed and unsigned binary numbers, don't get me started, i'm still traumatised my my exam where we had to work out floating point (I think it was signed too, but I honestly can't remember :lol: )

it was all convert this to that, and back again lol
Signed is usually 2's complement -- NOT the bits and add 1. So it isn't too hard to encode/decode
For the floating point look at the "IEEE floating point standard"->http://en.wikipedia.org/wiki/IEEE_754-1985
There's everything you need to know there.

Generally, there are 23 bits dedicated to the fraction and 8 bits for the exponent, plus 1 sign convention bit for signed, or 9 bits for the exponent unsigned, yeilding a total of 32 bits(4bytes).

Image
from http://www.dspguide.com

God I wish I could get into a programming course, now I'm taking a computer literacy(CS introduction) course so I can take actual programming/EE courses.(thinking about software engineering)

I didn't have the option of a programming-related class in high-school(not enough students wanted it), and that sucks!





Did it help any? ;)

Re: std::string-to-int converter

Posted: Wed Jul 08, 2009 12:20 pm
by Ginto8
Netwatcher wrote:I also think I saw you in another forum, are you in bytesIT?
nope. The only nicks I've had for a while were gintosakate and then Ginto8. ;)

Re: std::string-to-int converter

Posted: Fri Jul 10, 2009 2:48 am
by eatcomics
Wow you guys, I like seeing this kind of stuff, I'm sick of the C++ "I need code help stuff" I want more low level! && Hardware! ;) Maybe the owners can make a few more dedicated threads wink wink...

Re: std::string-to-int converter

Posted: Fri Jul 10, 2009 1:52 pm
by programmerinprogress
eatcomics wrote:Wow you guys, I like seeing this kind of stuff, I'm sick of the C++ "I need code help stuff" I want more low level! && Hardware! ;) Maybe the owners can make a few more dedicated threads wink wink...
yeah me too, i'm very much interested in the low-level stuff as a side-project type thing.

some times it's fun to mess around with 1's and 0's :)