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
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 »

I agree completely! :)
Image
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 »

programmerinprogress wrote:
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 recommend learning Assembly if you haven't already. It's really fun and you really learn a lot about computers.

http://www.drpaulcarter.com/pcasm/index.php has a great, free, modern book on Assembly.

Assembly is fun to learn, but it is really a pain in the ass to actually make useful stuff in. ;)
Last edited by Joeyotrevor on Sat Jul 11, 2009 2:35 pm, edited 1 time in total.

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
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 »

It's cool I know quite a bit about assembly :) I think the other people do to...
Image
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 »

Joeyotrevor wrote:
programmerinprogress wrote:
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 recommend learning Assembly if you haven't already. It's really fun and you really learn a lot about computers.

http://www.drpaulcarter.com/pcasm/index.php has a great, free, modern book on Assembly.

Assembly is fun, but it is really a pain in the ass to actually make useful stuff in. ;)
what assembly exactly?
"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
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 »

It's a second generation programming language, they invented it back in the 60's to cut down on the alarming rate of nervous breakdowns that computer operators were sufferring from by tapping in 1's and 0's all day :lol:

But, being serious now, it's a form of programming language which uses mnemonics (aides to the memory) to carry out machine code instructions.

Until assembly came along, you were expected to flick switches on the front panel of your computer terminal, and that was fine for small programs, but as programs grew in size, so did the demand for languages that simplifed the understanding of code, thus assembly was born

In assembly, you spend most of your time either loading or storing numbers from different memory locations (wither registers in your CPU or memory in your computer), and then doing some arithmetic, such as adding, subtraction, multiplying and division, you also do bitwise operations on numbers such as AND, OR, NOT etc, oh and bit shifting.

you have branching statements which are the equivalents to if, else if and else, the way all of the things mentioned above are implemented into the assembler vary depending on the CPU architecture you use, the most straightforward one I've tried is Acorn RISC assembly on an Acorn Archemedies, it had 14 registers to mess around with, and the mnemonics it used were easy to understand, it was also 32-bit, so you could do quite a few operations without the monotomy of having to do several operations for multiplication, because there was a multiplication instruction, which took a few operands at once.

PS: i'm not expert by the way, i've only been introduced to assembly, I believe Gyro is a lot more acquainted with the assembler than me :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
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 »

programmerinprogress wrote:It's a second generation programming language, they invented it back in the 60's to cut down on the alarming rate of nervous breakdowns that computer operators were sufferring from by tapping in 1's and 0's all day :lol:

But, being serious now, it's a form of programming language which uses mnemonics (aides to the memory) to carry out machine code instructions.

Until assembly came along, you were expected to flick switches on the front panel of your computer terminal, and that was fine for small programs, but as programs grew in size, so did the demand for languages that simplifed the understanding of code, thus assembly was born

In assembly, you spend most of your time either loading or storing numbers from different memory locations (wither registers in your CPU or memory in your computer), and then doing some arithmetic, such as adding, subtraction, multiplying and division, you also do bitwise operations on numbers such as AND, OR, NOT etc, oh and bit shifting.

you have branching statements which are the equivalents to if, else if and else, the way all of the things mentioned above are implemented into the assembler vary depending on the CPU architecture you use, the most straightforward one I've tried is Acorn RISC assembly on an Acorn Archemedies, it had 14 registers to mess around with, and the mnemonics it used were easy to understand, it was also 32-bit, so you could do quite a few operations without the monotomy of having to do several operations for multiplication, because there was a multiplication instruction, which took a few operands at once.

PS: i'm not expert by the way, I've only been introduced to assembly, I believe Gyro is a lot more acquainted with the assembler than me :lol:
:lol: I meant it quite literally;
Netwatcher wrote:What assembly exactly
.
As you know, assembly is a family of languages as in assembly 8086 and whatnot...
Co's most of them are a pain in the ass rather then "fun".(8086 got too many stuff going on because of it's "backwards compatibility" shit ...)

Anyway... I think some people might get edjucated by your post so meh... :)
"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
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: std::string-to-int converter

Post by MarauderIIC »

programmerinprogress wrote: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 [using bitmasks]
...because subtracting 48 is simply too hard to understand :)

As for the floating-point numbers:

The number is always assumed to be "+1 point something times 2 to the (something minus 127) power". So with the exponent-mantissa notation that netwatcher posted,
Click here to see the hidden message (It might contain spoilers)
Image
, you take "1". You append the mantissa (binary decimal) to "1". You convert it the same way you do a decimal number. Recall, when you convert decimal numbers, that things like 0.4 are the same as 4 * 10^(-1).

So, a mantissa of
1100 0000 0000 0000 0000 000
is
1 * 2^(-1) + 1 * 2^(-2) + 0 * 2^(-3) + 0 * 2 ^ (-4) + .... + 0 * 2 ^ (-23)
Which is
1/2 + 1/4 + 0 + 0 + 0....
0.5 + 0.25
0.75

And since we make the assumption about "1", this means that the decimal that we multiply by "2 to the (something minus 127) power" is 1.75

Now we need the exponent part:
0000 0111
Which is
0 * 2^7 + 0 * 2^7 + 0 * 2^6 + ... + 0 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0
= 0 + 0 + 0 + ... + 0 + 1*4 + 1*2 + 1*1 = 7

The exponent is assumed to be in (theExponent - 127) form, and so, the exponent is 7 - 127 = -120. And again, the number as a whole is "1 point something times 2 to the (something minus 127) power". So our number is "1 point 75 times 2 to the negative 120th power" and becomes 1.75 * 2(-120) which hopefully comes out to 1.316554 * 10^(-36), since that's what the solution is in the example image above.

In short, floating-point notation has some rules -- such as "we will subtract 127 from the exponent specified" and "make the leading digit in the decimal be 1" which allows it to represent numbers in its limited number of bits, and books seem to neglect mentioning these. All this conversion is one reason that accuracy can be lost when dealing with floating-points.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply