🔒 Locked
string to int
Advertisement
Original Post
quote:
Original post by Craazer
So how can I convert string to int or double?
You have a lot of options here: The atoi function, the boost::lexical_cast operator, the stringstream class are only three of them.
quote:
Converting backwards isn''t a problem becose I use wsprintf() for that, or is it a good solution?
wsprintf is Windows only, sprintf (or snprintf) is more portable. There are a lot of options for you here as well: the boost::lexical_cast operator, the stringstream class are only a few of them.
Update GameDev.net system time campaign - success at last
string to integer works similarly, by the way
quote:Google does. ''"string to int" c'' gave immediately results. If people only did at least the minimum amount of searching before asking... maybe Gamedev forums wouldn''t exist then though.
Original post by Craazer
Hi! This is probably often asked queston but the search engine doesn''t work right now...
quote:Google does. ''"string to int" c'' gave immediately results. If people only did at least the minimum amount of searching before asking... maybe Gamedev forums wouldn''t exist then though.
Original post by civguy
[quote]Original post by Craazer
Hi! This is probably often asked queston but the search engine doesn''t work right now...
Probably would''t and the bad thing when searhing whit google is that strings and ints are in many langueges like java (what was the first search result).
However it seems that stringstream class is a good solution...
So great I can use that then, but when posting this thread I was hoping to learn about the actual technique as practice.
And when I said string I actualy ment string types and char arrays or pointers as strings, but I gues you can copy char arrays to string types.
just.. do it yourself..
read the first char. is it a number, get the number out of it. read next char, if its a number, multiply current value by 10, add new number to it, etc..
something like this. i''m very very very tierd..:D
"take a look around" - limp bizkit
www.google.com
read the first char. is it a number, get the number out of it. read next char, if its a number, multiply current value by 10, add new number to it, etc..
bool isDigit(char chr) { return chr >= ''0'' && chr <= ''9''; }
char* chr = beginning_of_string;
while(*chr) { if(isDigit(chr)) break; ++chr; }
int number = 0;
do {
number *= 10;
number += *chr - ''0'';
chr++;
}while(*chr && isDigit(chr));
something like this. i''m very very very tierd..:D
"take a look around" - limp bizkit
www.google.com
quote:
Original post by davepermen
just.. do it yourself..
read the first char. is it a number, get the number out of it. read next char, if its a number, multiply current value by 10, add new number to it, etc..
bool isDigit(char chr) { return chr >= ''0'' && chr <= ''9''; }
char* chr = beginning_of_string;
while(*chr) { if(isDigit(chr)) break; ++chr; }
int number = 0;
do {
number *= 10;
number += *chr - ''0'';
chr++;
}while(*chr && isDigit(chr));
something like this. i''m very very very tierd..:D
"take a look around" - limp bizkit
www.google.com
Thanks, actualy I just ended trying to do shometing similar becose the dot was kinda difficult to handle becose my solution took lot of speed. Well will be trying again later becose im very very tired too
itoa()
atoi()
other handy things ltoa(), atol(), etc
funny thing tho.. ftoa is missing, there is a atof tho.
itoa/ltoa do take a few extra things... a buffer to put string in... then the number, then a radix, usualy 10 (10 base number system is what we humans like to count in anyway
atoi()
other handy things ltoa(), atol(), etc
funny thing tho.. ftoa is missing, there is a atof tho.
itoa/ltoa do take a few extra things... a buffer to put string in... then the number, then a radix, usualy 10 (10 base number system is what we humans like to count in anyway
just looking at ''Thinking in C++'' and found this code example:
//: C03:stringConv.h
// Chuck Allison''s string converter
#ifndef STRINGCONV_H
#define STRINGCONV_H
#include <string>
#include <sstream>
template<typename T>
T fromString(const std::string& s) {
std::istringstream is(s);
T t;
is >> t;
return t;
}
template<typename T>
std::string toString(const T& t) {
std::ostringstream s;
s << t;
return s.str();
}
#endif // STRINGCONV_H ///:~
quote:
Here’s a test program, that includes the use of the Standard Library complex number type:
//: C03:stringConvTest.cpp
#include "stringConv.h"
#include <iostream>
#include <complex>
using namespace std;
int main() {
int i = 1234;
cout << "i == \"" << toString(i) << "\"\n";
float x = 567.89;
cout << "x == \"" << toString(x) << "\"\n";
complex<float> c(1.0, 2.0);
cout << "c == \"" << toString(c) << "\"\n";
cout << endl;
i = fromString<int>(string("1234"));
cout << "i == " << i << endl;
x = fromString<float>(string("567.89"));
cout << "x == " << x << endl;
c = fromString< complex<float> >(string("(1.0,2.0)"));
cout << "c == " << c << endl;
} ///:~
quote:
The output is what you’d expect:
i == "1234"
x == "567.89"
c == "(1,2)"
i == 1234
x == 567.89
c == (1,2)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement