Ways to convert a number to a string and vice versa

 Many times in programming you will be in a condition where you will have to convert a string to a number and also a number to a string, if you are repleted with the knowledge of the various ways in which you can do this then you can skip this blog, cause these are some basic functions which everyone should know at least in my opinion.

To convert a string to a number:-

1) Use stoi() or atoi() functions:- These are very simple basic kind of functions. Assume a string str.

                                                         The syntax is:- int a;a=stoi(str);

2) Use boost lexical cast method:- This is a bit uncommon but is extremely useful. In this you can simply 

                                                          parse values from any data type to any other one. By syntax like 

                                                          boost::lexical_cast<dataTypeRequired>(valueOfAnyDataType);

                                                          This will return the converted value.

To convert a number to a string:-

1) Use to_string() function:- This one is my favourite cause it is very logical and I have used this quite                                                                                                often.

2) Use boost lexical cast method:- Again you can use this one, but I personally don't use this much.

Other than this there are also methods of stringstream which are not needed but still they should be known, although I have some grey areas in that, which I will have to clear afterwards. 

I couldn't blog for two days, cause the schedule was a little bit tight, but I try to finish every task for the day, thinking about this I have my spanish lessons left for the day and I haven't had a walk too for today, so I will have to do that now. 

Comments

Popular Posts