4

Where can I find an overview of type conversion, e.g. string to integer, etc.?

Because of the comments so far, I'll clarify: I'm looking for a list /table that says: To convert a string to an int, use: ... And same for other data types (where possible): double to int, char to string, ...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    C++ has no string to integer conversion as part of the language, so that will be a very short summary. Perhaps you could post a more specific question? – bmargulies Mar 01 '10 at 14:03
  • 5
    Jeeze, you guys are harsh. This seems like a perfectly reasonable - if a little short - question. Perhaps not worth a vote up, but certainly not worth SIX down votes. – Daniel Bingham Mar 01 '10 at 14:16
  • 1
    @anthares, +1 for beating me to it :) – Cam Mar 01 '10 at 14:52
  • We didn't want to discourage you from posting here. But, maybe reading the FAQs is a good idea first. You can't expect posting so general question as your first revision and get any sensible answers. Take your time to consider what exactly you're asking and others will take their time to answer you. Show your respect, to get some. – anthares Mar 01 '10 at 15:09
  • 1
    @dbingham: Yes, but seven up and six down (what I'm looking at) is +58 rep, which is pretty good for a simple question. I think what's happening is that lots of people are going for the Electorate badge, which rewards voting on questions (and it doesn't have to be upvoting them). – David Thornley Mar 01 '10 at 15:14
  • 1
    @anthares You imply that I cannot expect a sensible answer, have not taken my time and am not showing respect. I hope you don't treat everyone like this. – Reinstate Monica - Goodbye SE Mar 01 '10 at 15:14
  • Oh, and from FAQ, "No question is too trivial or too "newbie"." – Reinstate Monica - Goodbye SE Mar 01 '10 at 15:19
  • But there a too general questions, right ? I don't like wasting time arguing. Have fun ! And do not blame me personally, looks like I'm not the only one, who doesn't like your question. – anthares Mar 01 '10 at 15:25
  • 2
    @Mark: well, the fact is that your original question doesn’t make much sense to someone with an understanding of how C++ works, although it actually *does* make sense once you assume that the asker doesn’t already know the answer (i.e. C++ doesn’t define a comprehensible list of valid casts the way other languages do). I guess people got hung up on that, especially since googling for your question title yields seemingly good results. Why they didn’t help only became clear after your clarification. – Konrad Rudolph Mar 01 '10 at 15:28
  • 1
    @David Thornley I don't think the question deserved reputation either way. Just a clean, clear and concise answer. The question isn't so bad it deserves so many downvotes - but not really good enough to merit up votes. A few comments maybe explaining that more detail was required for a really good answer. The sarcastic "google.com" answers and down votes do nothing to help the questioner learn either the answer he sought or how to better word his questions. And learning is what StackOverflow is all about. – Daniel Bingham Mar 01 '10 at 20:39
  • 1
    @Konrad Rudolph; @dbingham. Thank you for your clear & kind responses. I hope some other responders are able to learn from your way of answering. – Reinstate Monica - Goodbye SE Mar 02 '10 at 08:26
  • Oh - and shouldn't there be a rule, similar to Wikipedia, of Don't Bite The Newcomers? http://en.wikipedia.org/wiki/Wikipedia:Please_do_not_bite_the_newcomers – Reinstate Monica - Goodbye SE Mar 02 '10 at 08:28
  • 1
    @Mark: there should, absolutely. … in fact, there *is*: http://meta.stackexchange.com/questions/9953 – but as on Wikipedia, the rule cannot be enforced. Look at the bright side: you got 90 reputation points out of your first question; that’s not too shabby. – Konrad Rudolph Mar 02 '10 at 09:03

4 Answers4

11

If it's string to/from other types then stringstream or boost::lexical_cast.

For other types it will depend on the types, but maybe look up the standard cast templates? static_cast and dynamic_cast should do most things you need, or there is const_cast and reinterpret_cast which tend to only be useful for dealing with legacy systems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jk.
  • 13,817
  • 5
  • 37
  • 50
  • 1
    I don't mean to self promote, but for reference this answer shows how to use `lexical_cast`, and how to make a simple no-boost replacement with streams as well. http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/1243435#1243435 – GManNickG Mar 01 '10 at 15:54
1

Streams are essentially C++'s string conversion operators.

You also have available C's conversion method of sprintf, but that is massively error-prone and unsafe.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
0

There are functions in the standard library to do string<->int conversions. You should grab any reference book on C++, or search Google.

I'm more familiar with C, but I believe the C++ functions are the same: atoi, strtoi, etc.

CWF
  • 2,067
  • 1
  • 13
  • 4
-1

I would recommend to read the reference about the string class and then something about casting to different data types generally.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pyjong
  • 3,095
  • 4
  • 32
  • 50