std::string-to-int converter

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: std::string-to-int converter

Post 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:
Last edited by programmerinprogress on Tue Jul 07, 2009 6:43 pm, edited 1 time in total.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: std::string-to-int converter

Post 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!
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: std::string-to-int converter

Post 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!
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: std::string-to-int converter

Post by Joeyotrevor »

Actually bitwise NOT is ~, ! is a boolean NOT.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: std::string-to-int converter

Post 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)
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: std::string-to-int converter

Post 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.
Last edited by Ginto8 on Tue Jul 07, 2009 6:44 pm, edited 1 time in total.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: std::string-to-int converter

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: std::string-to-int converter

Post 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
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: std::string-to-int converter

Post 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?
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: std::string-to-int converter

Post 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 :(.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: std::string-to-int converter

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: std::string-to-int converter

Post 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? ;)
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: std::string-to-int converter

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: std::string-to-int converter

Post 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...
Image
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: std::string-to-int converter

Post 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 :)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply