Wednesday, May 8, 2013

Convert an Integer into a String in c++

Convert an Integer into a String  in c++





using namespace std;
#include <string>
#include <sstream>
#include <iostream>

template <class T>
std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
{
  std::ostringstream oss;
  oss << f << t;
  return oss.str();
}

int main()
{
  // the second parameter of to_string() should be one of 
  // std::hex, std::dec or std::oct
  std::cout<<to_string<long>(123456, std::hex)<<std::endl;
  std::cout<<to_string<long>(123456, std::oct)<<std::endl;
  system("pause");
  return 0;
} 

----------------------------------------------------------

string to numeric

float stof(const string& str, size_t *idx = 0);double stod(const string& str, size_t *idx = 0);long double stold(const string& str, size_t *idx = 0);int stoi(const string& str, size_t *idx = 0, int base = 10);long stol(const string& str, size_t *idx = 0, int base = 10);unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);long long stoll(const string& str, size_t *idx = 0, int base = 10);unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);

numeric to string

string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float val); string to_string(double val); string to_string(long double val);



----------------------------------------------
Easiest way to convert int to string in C++
c++ - Converting an int to std::string
Converting numbers to strings and string - C++
convert between std::string and int - C++
C++ String: How to convert a numeric type to a string
Searches related to Convert an Integer into a String in c++
string to int c++
c++ stdlib h
parse string to int c++
c++ get int from string

No comments:

Post a Comment