5 Replies - 1753 Views - Last Post: 18 September 2010 - 11:15 AM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Ways to convert char/string to numbers, vice-versa

Posted 17 September 2010 - 08:44 PM

I'm wondering what ways are recommended to do some conversions between different data types, in particular chars to numbers and vice-versa. I've been using the funtions atoi and atof for char to int/double, but I think that is the C way to do it. What is a good way to do that type of conversion without using the functions I mentioned before?

Also, what is the recommended way to convert strings to doubles/ints, etc? I've seen ways to do it with string streams.. but I'm curious if there are other ways.

I appreciate any expert advise. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Ways to convert char/string to numbers, vice-versa

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,879
  • Joined: 10-May 07

Re: Ways to convert char/string to numbers, vice-versa

Posted 17 September 2010 - 08:46 PM

Using stdlib.h atoi will convert ascii to integer. & I prefer sprintf to go the other way around.
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,087
  • Joined: 30-May 10

Re: Ways to convert char/string to numbers, vice-versa

Posted 17 September 2010 - 10:33 PM

For C, the strtod(3), strtol(3), strtoul(3) functions provide the best conversion options since they also detect in a meaningful way things like overflow and out of range.
Was This Post Helpful? 1
  • +
  • -

#4 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Ways to convert char/string to numbers, vice-versa

Posted 18 September 2010 - 12:01 AM

this is a possible int to string, though i think its only works in C++

#include <sstream>

void main()
{
    int number = 123;
    std::string retVal;
    std::stringstream out;
    out<<number;
    retVal = out.c_str();

}

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Ways to convert char/string to numbers, vice-versa

Posted 18 September 2010 - 01:33 AM

View Postnautica17, on 18 September 2010 - 12:44 PM, said:

I've seen ways to do it with string streams


Allowing this was one of the prime reasons stringstreams were introduced so using them is often considered the "canonical" way in C++

View PostLarsonator, on 18 September 2010 - 04:01 PM, said:

void main()
{
 ...
}


"void main()" is wrong. Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.

Was This Post Helpful? 2
  • +
  • -

#6 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Ways to convert char/string to numbers, vice-versa

Posted 18 September 2010 - 11:15 AM

Thank-you for the advise everyone! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1