Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

from int to string

This topic is 4933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Assuming your talking about C++ you can use string streams to parse to & from strings in memory, e.g.:


#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>
#include <iostream>

int main() {

std::istringstream iss("123123 4534 656456 6757 5345 23423");

std::vector<int> v((std::istream_iterator<int>(iss)),
std::istream_iterator<int>());

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));


}

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshnathan
wow, that seems complicated, isn't there an easier way to go around it?


its not complicated at all, its actually very elegant but anyways:


#include <sstream>
#include <iostream>

int main() {

std::istringstream iss("2342");

int i = 0;

iss >> i;

std::cout << i << std::endl;

}


The reason why i didn't show this in the first place was i wanted to show whats possible (something with some meat) and not just the above.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshnathan
I would like to go from int to string not the other way around, how to? :D



#include <string>
#include <sstream>
#include <iostream>

int main() {

const int i = 343243;
const float j = 4354.5f;

std::ostringstream iss;

iss << "an int:" << i << ", a float: " << j << ", a double: " << 3.4;

std::string s = iss.str();

std::cout << s << std::endl; //or std::cout << iss.str() << std::endl;

}

Share this post


Link to post
Share on other sites
You can write your own utility function called toString and reuse it whenever you need to. I'll show you the specific version for converting integers to strings and then generalise it for any type which supports the << ostream operator.


//toString.h

#include <sstream>

std::string ToString(int n) {
std::stringstream ss;
ss << n;
return ss.str();
}



The general version makes use of templates, so you write it for a general type, whether that's an int, double, std::complex, MyClass type, whatever and it works:

//toString.h

#include <sstream>

template<typename T>
std::string ToString(const T& t) {
std::stringstream ss;
ss << t;
return ss.str();
}


I made ToString take a constant reference as it is generally more efficient to pass objects by reference than by value (although for built-in types like int and doubles its not going to be any more or less efficient).

Usage:

#include <string>
#include <complex>
#include "ToString.h"

int main() {
std::string n = ToString(5);
std::string f = ToString(5.555f);
std::string d = ToString(5.566745235);
std::string c = ToString(std::complex<double>(1.2, 5.6));
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Quote:
Original post by petewood
..."String Goodness"...

That's a good one.


You can also do the other direction - ToInt, or generalised ToType:


//ToType.h
template<typename T>
T ToType(const std::string& s) {
std::stringstream ss;
ss << s;

T result;
ss >> result;
return result
}



Usage:

#include <complex>#include "ToType.h"

int main() {
int n = ToType<int>("1812");
double d = ToType<double>("3.1412");
std::complex<double> c = ToType<std::complex>("(1.2, 3.7)");
return 0;
}




The only problem with this is if you convert a string into a string (heh, we all make mistakes... or maybe with generic code you wouldn't know what you were going to pass to the function). If the string contains spaces it will get broken up at the first space and you'll lose the rest of it. So you need to provide a specialisation, or an overload, just for strings. In it you just return a copy of the string that was passed to the function:


//ToType.h

//...previous code

std::string ToType<std::string>(const std::string& s) {
return s;
}




All of this has already been done better in the boost library lexical_cast

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!