Re: Converting from string to integer, integer to string, string to float, float to string

From: ak (ak_at_workmail.com)
Date: 10/31/04


Date: Sun, 31 Oct 2004 23:00:58 +0800

On Mon, 1 Nov 2004 01:21:05 +1100, "Nicolae Fieraru"
<nospam@please.com> wrote:

>Hi All,
>
>How can I perform these conversion functions? I use Visual C++ 6.0 and I
>want to use available libraries, not to go downloading some other ones from
>the web and not to do some myself.
>
>I found myself StrToInt and StrToInt64Ex. How about the other ones?
>
>
>Regards,
>Nicolae
>

// int to str

stringstream ss;
int n = 123;
ss << n;
string s = ss.str();

// str to int

string s = "12";
istringstream is(s);
int n;
is >> n;

// str to float

string s = "1.12";
istringstream is(s);
double d;
is >> d;

// float to str

double floatnumber = 2.013;
ss << floatnumber;
string s = ss.str();

hth/ak



Relevant Pages

  • Re: parser needed
    ... static int tokenlen(const char *str, ... str - string containing expression ... ignores white space between tokens ...
    (comp.programming)
  • Linker error and no classes appear
    ... int main ... {string word1, word2, temp; ... string::string(const string &str) ...
    (microsoft.public.vc.language)
  • implicit declaration of function error
    ... void func(string str) { ... void func2{ ... implicit declaration of function `int func' ... cannot pass objects of type `string' through `...' ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Why date do not construct from date?
    ... Why int form int, str from str, Decumal from Decumal can construct ... instead it creates an int from a string. ... constructor for the date object. ...
    (comp.lang.python)
  • Re: How to Return an array of strings in C (char**)
    ... int start=0; ... // to know the number of elements in the array ... The reason is that you fail to increment str. ... The you could have printed "here" before entering the main loop to see if you actually got there, then str + start to see where you go to in the string on each pass. ...
    (comp.lang.c)