| | |
Converting a numerical value to a string and vice-versa
These functions, to some, may seem trivial, but many people have queried in the forums on this topic numerous times.
The functions are templated, so that it can accommodate any data-type that the user wishes to use (For example, the same function can be used for converting variables of type
The first function, Fn I (
Sample Function Call:
The second function, Fn II (
Sample Function Call:
*Note*: You will have to include the header files -
Hope this snippet helps....
The functions are templated, so that it can accommodate any data-type that the user wishes to use (For example, the same function can be used for converting variables of type
long , int , double etc into strings, and vice-versa).The first function, Fn I (
convertToString ), is capable of converting a variable of any data-type into a string.Sample Function Call:
C++ Syntax (Toggle Plain Text)
string MyInt = convertToString<int>(10);
The second function, Fn II (
convertFromString ), can convert a string into a variable of any data-type.Sample Function Call:
C++ Syntax (Toggle Plain Text)
int MyNumber = convertFromString<int>("45");
*Note*: You will have to include the header files -
<sstream> and <string.h> for the code to work...Hope this snippet helps....
/* This Function (Fn I) converts a variable of data-type DataType into a string */ template <typename DataType> string convertToString(DataType MyValue) { // Creates a Output String Stream to hold // the variable passed (MyValue) ostringstream OutStream; // Enter the variable into the stream OutStream << MyValue; // Return the stream as an array of // characters(string) return (OutStream.str()); } /* This Function (Fn II) converts a string into a variable of data-type DataType */ template <typename DataType> DataType convertFromString(string MyString) { // Stores the return value DataType retValue; // Creates an Input String Stream to extract // the contents of the string istringstream InStream(MyString); // Read contents of String and store in retValue InStream >> retValue; // Return the value in the desired DataType return retValue; }
Similar Threads
- converting from binary to decimal and vice versa (C)
- HELP!!! Converting Hexadecimal to Decimal or vice versa (MS SQL)
- Code Snippet: Converting Fahrenheit-Celsius & Vice-Versa (C)
- How to connection string into a dictionary in php code and vice versa (PHP)
- Converting byte value into integer and vice versa (C++)
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets




