converting one type to another

i see numerous different ways of converting one type to another. static_cast<int>(some_float) will convert the float to an int, but yet it doesnt convert for example a string to an int or vice versa.

Googling the question i find forums in which people give a response to this as bitshifting, or looping through the chars of the string to convert each, or some response that a beginner wouldnt understand. So then the beginner has to learn all that jsut to do a simple type conversion.

Coming from python i am use to the builtin functions int() which converts any digital type of value to an int, or str(), which converts any data type to an explicit string. So i am surprised that c++ doesnt have some simple method like this? or does it? I am a beginner so i am not looking at the fastest process or even the "best" process, i am just looking for the easiest process, and ill work on the faster method later when i feel more comfortable in c++.

the basic methods i was looking for an would most use as a beginner:

char to a string
string to a char
int to a string
string to an int
float to an int
int to a float

it seems like there should be some built in function to take in any type value as one arg, and a second arg to the wanted convert type and return the first arg as the type you specified.

and one of the most used ways i as a beginner would want such a function is for getting user input. Which it appears getline(cin, input_var) is the only method that will get "all" of the users line, so then i have to convert the string to a float,int, or char to do things with it, while in addition of checking that they input the correct data type i am looking for.
Last edited on
boost::lexical_cast seems to be the closest to what you're looking for
ok i sorry to plaster all results for the repo search, but which one of these are the boost that you refer to?
libboost-dev ?

Also is there no std way?


metulburr@ubuntu:~$ sudo apt-cache search libboost
[sudo] password for metulburr:
libboost-dbg - Boost C++ Libraries with debug symbols
libboost-dev - Boost C++ Libraries development files (default version)
libboost-doc - Boost C++ Libraries documentation (default version)
libboost-graph1.46-dev - generic graph components and algorithms in C++
libboost-graph1.46.1 - generic graph components and algorithms in C++
libboost-iostreams-dev - Boost.Iostreams Library development files (default version)
libboost-iostreams1.46-dev - Boost.Iostreams Library development files
libboost-iostreams1.46.1 - Boost.Iostreams Library
libboost-program-options-dev - program options library for C++ (default version)
libboost-program-options1.46-dev - program options library for C++
libboost-program-options1.46.1 - program options library for C++
libboost-python-dev - Boost.Python Library development files (default version)
libboost-python1.46-dev - Boost.Python Library development files
libboost-python1.46.1 - Boost.Python Library
libboost-regex1.46-dev - regular expression library for C++
libboost-regex1.46.1 - regular expression library for C++
libboost-serialization-dev - serialization library for C++ (default version)
libboost-serialization1.46-dev - serialization library for C++
libboost-serialization1.46.1 - serialization library for C++
libboost-test-dev - components for writing and executing test suites (default version)
libboost-test1.46-dev - components for writing and executing test suites
libboost-test1.46.1 - components for writing and executing test suites
libboost1.46-dbg - Boost C++ Libraries with debug symbols
libboost1.46-dev - Boost C++ Libraries development files
libboost1.46-doc - Boost.org libraries documentation
libboost-all-dev - Boost C++ Libraries development files (ALL, default version)
libboost-chrono1.48-dev - C++ representation of time duration, time point, and clocks
libboost-chrono1.48.0 - C++ representation of time duration, time point, and clocks
libboost-date-time-dev - date-time libraries based on generic programming (default version)
libboost-date-time1.46-dev - set of date-time libraries based on generic programming concepts
libboost-date-time1.46.1 - set of date-time libraries based on generic programming concepts
libboost-date-time1.48-dev - set of date-time libraries based on generic programming concepts
libboost-date-time1.48.0 - set of date-time libraries based on generic programming concepts
libboost-filesystem-dev - filesystem operations in C++ (default version)
libboost-filesystem1.46-dev - filesystem operations (portable paths, iteration over directories, etc) in C++
libboost-filesystem1.46.1 - filesystem operations (portable paths, iteration over directories, etc) in C++
libboost-filesystem1.48-dev - filesystem operations (portable paths, iteration over directories, etc) in C++
libboost-filesystem1.48.0 - filesystem operations (portable paths, iteration over directories, etc) in C++
libboost-graph-dev - generic graph components and algorithms in C++ (default version)
libboost-graph-parallel-dev - generic graph components and algorithms in C++ (default version)
libboost-graph-parallel1.46-dev - generic graph components and algorithms in C++
libboost-graph-parallel1.46.1 - generic graph components and algorithms in C++
libboost-graph-parallel1.48-dev - generic graph components and algorithms in C++
libboost-graph-parallel1.48.0 - generic graph components and algorithms in C++
libboost-graph1.48-dev - generic graph components and algorithms in C++
libboost-graph1.48.0 - generic graph components and algorithms in C++
libboost-iostreams1.48-dev - Boost.Iostreams Library development files
libboost-iostreams1.48.0 - Boost.Iostreams Library
libboost-locale1.48-dev - C++ facilities for localization
libboost-locale1.48.0 - C++ facilities for localization
libboost-math-dev - Boost.Math Library development files (default version)
libboost-math1.46-dev - Boost.Math Library development files
libboost-math1.46.1 - Boost.Math Library
libboost-math1.48-dev - Boost.Math Library development files
libboost-math1.48.0 - Boost.Math Library
libboost-mpi-dev - C++ interface to the Message Passing Interface (MPI) (default version)
libboost-mpi-python-dev - Python interface to the Message Passing Interface (MPI) (default version)
libboost-mpi-python1.46-dev - C++ interface to the Message Passing Interface (MPI), Python Bindings
libboost-mpi-python1.46.1 - C++ interface to the Message Passing Interface (MPI), Python Bindings
libboost-mpi-python1.48-dev - C++ interface to the Message Passing Interface (MPI), Python Bindings
libboost-mpi-python1.48.0 - C++ interface to the Message Passing Interface (MPI), Python Bindings
libboost-mpi1.46-dev - C++ interface to the Message Passing Interface (MPI)
libboost-mpi1.46.1 - C++ interface to the Message Passing Interface (MPI)
libboost-mpi1.48-dev - C++ interface to the Message Passing Interface (MPI)
libboost-mpi1.48.0 - C++ interface to the Message Passing Interface (MPI)
libboost-program-options1.48-dev - program options library for C++
libboost-program-options1.48.0 - program options library for C++
libboost-python1.48-dev - Boost.Python Library development files
libboost-python1.48.0 - Boost.Python Library
libboost-random-dev - Boost Random Number Library (default version)
libboost-random1.46-dev - Boost Random Number Library
libboost-random1.46.1 - Boost Random Number Library
libboost-random1.48-dev - Boost Random Number Library
libboost-random1.48.0 - Boost Random Number Library
libboost-regex-dev - regular expression library for C++ (default version)
libboost-regex1.48-dev - regular expression library for C++
libboost-regex1.48.0 - regular expression library for C++
libboost-serialization1.48-dev - serialization library for C++
libboost-serialization1.48.0 - serialization library for C++
libboost-signals-dev - managed signals and slots library for C++ (default version)
libboost-signals1.46-dev - managed signals and slots library for C++
libboost-signals1.46.1 - managed signals and slots library for C++
libboost-signals1.48-dev - managed signals and slots library for C++
libboost-signals1.48.0 - managed signals and slots library for C++
libboost-system-dev - Operating system (e.g. diagnostics support) library (default version)
libboost-system1.46-dev - Operating system (e.g. diagnostics support) library
libboost-system1.46.1 - Operating system (e.g. diagnostics support) library
libboost-system1.48-dev - Operating system (e.g. diagnostics support) library
libboost-system1.48.0 - Operating system (e.g. diagnostics support) library
libboost-test1.48-dev - components for writing and executing test suites
libboost-test1.48.0 - components for writing and executing test suites
libboost-thread-dev - portable C++ multi-threading (default version)
libboost-thread1.46-dev - portable C++ multi-threading
libboost-thread1.46.1 - portable C++ multi-threading
libboost-thread1.48-dev - portable C++ multi-threading
libboost-thread1.48.0 - portable C++ multi-threading
libboost-timer1.48-dev - C++ wall clock and CPU process timers
libboost-timer1.48.0 - C++ wall clock and CPU process timers
libboost-wave-dev - C99/C++ preprocessor library (default version)
libboost-wave1.46-dev - C99/C++ preprocessor library
libboost-wave1.46.1 - C99/C++ preprocessor library
libboost-wave1.48-dev - C99/C++ preprocessor library
libboost-wave1.48.0 - C99/C++ preprocessor library
libboost1.46-all-dev - Boost C++ Libraries development files (ALL)
libboost1.48-all-dev - Boost C++ Libraries development files (ALL)
libboost1.48-dbg - Boost C++ Libraries with debug symbols
libboost1.48-dev - Boost C++ Libraries development files
libboost1.48-doc - Boost.org libraries documentation
metulburr@ubuntu:~$
Last edited on
I'm not familiar with that package system, but libboost-dev sounds lie what you'd need.

The standard c++ offers many different ways to perform each of the conversions you listed, but in general, lexical_cast is equivalent to converting through std::stringstream
well i have been piecing together a class with something that i am looking for using STD. I have came across stringstream and statc_cast before. Are the class methods below the proper way to convert some type to some type?

i am just missing some :

int to float
str to char

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <sstream>
#include <string>
using namespace std;


class Convert{
//requires #include <string>
//requires #include <sstream>
    public:
        string int2str(int inter);
        int str2int(string str);
        int flo2int(float floater);
        string cha2str(char ch);

};

string Convert::int2str(int inter){
	stringstream num_str;
	num_str << inter;
	return num_str.str();
}
int Convert::str2int(string str){
	int i;
	stringstream ss(str);
	ss >> i;
	return i;
}

int Convert::flo2int(float floater){
    //rounds down
    int i = static_cast<int>(floater);
    return i;
}

string Convert::cha2str(char ch){
    stringstream ss;
    string s;
    ss << ch;
    ss >> s;
    return s;

}

int main(){


}
Last edited on
The reason there are many ways to convert objects of different types to each other is that there are many meaningful conversions. You have to carefully define what kind of result do you want on what input.

Like your flo2int is a truncating conversion (it's not the same as "rounds down" as the comment says), but there are more ways to do it.

Note that your cha2str could be done simply as return string(1, ch);

what would the expected semantics of "str to char" be?
well the str to char i used the same method as char to string

1
2
3
4
5
6
7
char Convert::str2cha(string stringer){
    //convert from string to char
    stringstream ss;
    char c;
    ss << stringer;
    ss >> c;
    return c;


Note that your cha2str could be done simply as return string(1, ch);

Im not sure where the 1 comes from? I was meaning 'a' as character to string.

also i just found out about the (type)var, im not sure what it is called, but its seems easier just to
cout << (float)10/3; for the float/int conversions

oh thanks for all your help too, i forgot to mention.
Last edited on
Topic archived. No new replies allowed.