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

X

x

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

how can I convert a number such as 1944 to a character array?

thanks!
 
L

Leor Zolman

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

how can I convert a number such as 1944 to a character array?

thanks!

You can go "retro" with sprintf (that would be my choice, actually, being a
retro kind of guy), or use C++ streams if you prefer it:

#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;

int main()
{
int y = 1944;

char buffer[10];
sprintf(buffer, "%d", y);
cout << "using sprintf: " << buffer << endl;

ostringstream os;
os << y;
strcpy(buffer, os.str().c_str());
cout << "using ostringstream: " << buffer << endl;

return 0;
}

-leor
 
C

Christopher Benson-Manica

Leor Zolman said:
int y = 1944;
char buffer[10];
sprintf(buffer, "%d", y);

No problem in this case, but on my system, INT_MAX is 2147483647,
which is too big to fit in a 10-character buffer :)
 
M

Mike Wahler

x said:
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::eek:stringstream 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 << '\n';

delete array;
return 0;
}


-Mike
 
H

Howard

Mike Wahler said:
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 << '\n';

delete array;


delete [] array;

-Howard
 
M

Mike Wahler

Howard said:
Mike Wahler said:
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 << '\n';

delete array;


delete [] array;


Oops. Thanks.

-Mike
 
S

Siemel Naran

x said:
converting 1944 to '1','9','4','4'

how can I convert a number such as 1944 to a character array?

How about

const size_t N = std::numeric_limits<int>::digits10+1; // is this the right
one?
char array[N];
char * ptr = array;
for ( ; x; x/=10, ++ptr) {
int y = x%10;
*ptr = y + '0';
}
*ptr = 0;
 
J

Jeff Schwab

x said:
converting 1944 to '1','9','4','4'

how can I convert a number such as 1944 to a character array?

In addition to the many fine solutions already mentioned in this thread,
you might consider boost::lexical_cast.

http://www.boost.org/libs/conversion/lexical_cast.htm#lexical_cast

You can't actually convert to a raw array, but I find this quite readable:

boost::lexical_cast< std::string >( 1944 ).c_str( );

Or, of course:

lexical_cast< string >( 1944 ).c_str( );

-Jeff
 
M

Michiel Salters

Siemel Naran said:
x said:
converting 1944 to '1','9','4','4'

how can I convert a number such as 1944 to a character array?

How about

const size_t N = std::numeric_limits<int>::digits10+1; // is this the right
one?
char array[N];
char * ptr = array;
for ( ; x; x/=10, ++ptr) {
int y = x%10;
*ptr = y + '0';
}
*ptr = 0;

No, that produces 4491. You could do

const size_t N = std::numeric_limits<int>::digits10+1;
char array[N];
char * ptr = array+N-1;
*ptr = 0;
for ( ; x; x/=10, --ptr ) {
int y = x%10;
*ptr = y + '0';
}
std::cout << ptr << " == " << x << std::endl;

but char mucking is always tricky, and rarely needed.
boost::lexical_cast<std::string>( 1944 ) is easy.

Regards,
Michiel Salters
 
B

Buster

x said:
converting 1944 to '1','9','4','4'

how can I convert a number such as 1944 to a character array?

There's a reason there's no one-line way of doing this with the
standard library: it's very rarely necessary. There's also a reason
there's a one-line way of ouputting the characters to a file or
the console.
 
C

Christopher Benson-Manica

Rolf Magnus said:
Can you even imagine how much money that kind of "oops" has already
destroyed? :)

More easily than I can imagine how much it will destroy for future
generations ;)
 
L

Leor Zolman

Leor said:
Leor Zolman <[email protected]> spoke thus:

int y = 1944;
char buffer[10];
sprintf(buffer, "%d", y);

No problem in this case, but on my system, INT_MAX is 2147483647,
which is too big to fit in a 10-character buffer :)
Oops.

Can you even imagine how much money that kind of "oops" has already
destroyed? :)

I only really started using std::string in earnest following a series of
pathname-related overrun bugs in the "Proxy" CL.EXE I distribute with
STLFilt, and I must admit I niether use nor miss fixed-sized C-string
buffers much any more. The ill-advised undersized buffer in my original
response here probably resulted from my brain seeing "1944", thinking
"years", and then upping the size a bit more for "safety".

If I did this sort of thing a lot today, which I actually don't, then I'd
be worried in general about buffer overruns and probably abstract out the
T-to-string conversion into a utility template library. Here's a first cut,
along with a test driver:

//
// tostring.h:
// T-to-std::string and T-to-char* conversion library
//
// Synopsis:
//
// std::string toString(const T &t);
// Converts t to std::string via operator<<
//
// char *toCString(char *dest, const T&t);
// Converts T to C-style string at dest, returns
// dest (calls toString above).
//
// Version 0.1
// Leor Zolman, 4/14/2004
//

#ifndef TOSTRING_H
#define TOSTRING_H

#include <sstream>
#include <string>

template<typename T>
std::string toString(const T& t)
{
std::eek:stringstream os;
os << t;
return os.str();
}

template<typename T>
inline char *toCString(char *dest, const T&t)
{
return strcpy(dest, toString(t).c_str());
}

/*
// This one we'd only ever want for performance reasons:
std::string toString(const std::string &t)
{
return t;
}
*/

// And this covers the last pathological case:
inline const char *toCString(const char *t)
{
return t;
}

#endif



//
// tostrtest.cpp: Test tostring.h library
//

#include "tostring.h"

int main()
{
using namespace std;

char buffer[1000]; // ;-)

int y = 1944;
strcpy(buffer, toString(y).c_str());
cout << "from int: " << buffer << endl;
cout << "from int: " << toCString(buffer, y) << endl;

double d = 1944.44;
strcpy(buffer, toString(d).c_str());
cout << "from double: " << buffer << endl;
cout << "from double: " << toCString(buffer, d) << endl;

string s = "1944 was a great year";
strcpy(buffer, toString(s).c_str());
cout << "from a string: " << buffer << endl;
cout << "from a string: " << toCString(buffer, s) << endl;

strcpy(buffer, toString("this is a dumb").c_str());
cout << "from a char *: " << buffer << endl;
cout << "from a char *: " <<
toCString(buffer, "another dumb one") << endl;

return 0;
}


-leor
 
L

Leor Zolman

//
// tostrtest.cpp: Test tostring.h library
//

#include "tostring.h"

int main()

Sorry, forgot to put
#include <iostream>
up there. It was compiling for /me/ due to the headers included by
tostring.h in the platform I happen to be using (Comeau), but was rather
(un-)lucky.

Also, as mentioned by others in the thread by now, boost's lexical_cast
facility exists for this type of thing. I just like to practice my template
writing ;-)
-leor
 
S

Siemel Naran

Michiel Salters said:
but char mucking is always tricky, and rarely needed.
boost::lexical_cast<std::string>( 1944 ) is easy.

Thanks, didn't know about this one. Anyway, you need it if you're
implementing boost::lexical_cast or something like that. Also good to know
for interview questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top