Re: converting 1944 to '1','9','4','4'

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 04/13/04


Date: Tue, 13 Apr 2004 16:01:00 GMT


"x" <aotemp@hotmail.com> wrote in message
news:26d721d9.0404130708.5b217a1e@posting.google.com...
> converting 1944 to '1','9','4','4'
>
> how can I convert a number such as 1944 to a character array?
>
> thanks!

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    const int value(1944);
    std::ostringstream oss;
    oss << value;

    const std::string s(oss.str());
    const char *cs = s.c_str();
    const std::string::size_type sz(s.size());
    char *array = new char[sz];
    std::copy(cs, cs + sz, array);

    for(std::string::size_type i = 0; i < sz; ++i)
        std::cout << "array[" << i << "] == " << array[i] << '\n';

    delete array;
    return 0;
}

-Mike