By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

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

P: n/a
x
converting 1944 to '1','9','4','4'

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

thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
x wrote:
converting 1944 to '1','9','4','4'

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


Corvert it to string, then convert string into character array, if
string is not good enough for you.

See faq:
[38.1] How do I convert a value (a number, for example) to a std::string?
http://www.parashift.com/c++-faq-lit....html#faq-38.1
Jul 22 '05 #2

P: n/a
On 13 Apr 2004 08:08:24 -0700, ao****@hotmail.com (x) wrote:
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

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

P: n/a
Leor Zolman <le**@bdsoft.com> 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 :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #4

P: n/a

"x" <ao****@hotmail.com> wrote in message
news:26**************************@posting.google.c om...
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
Jul 22 '05 #5

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:09Uec.7605
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;


delete [] array;

-Howard
Jul 22 '05 #6

P: n/a
On Tue, 13 Apr 2004 15:36:30 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Leor Zolman <le**@bdsoft.com> 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.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:c5********@dispatch.concentric.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:09Uec.7605
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;


delete [] array;


Oops. Thanks.

-Mike
Jul 22 '05 #8

P: n/a
"x" <ao****@hotmail.com> wrote in message
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;

Jul 22 '05 #9

P: n/a
x wrote:
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...m#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
Jul 22 '05 #10

P: n/a
Leor Zolman wrote:
On Tue, 13 Apr 2004 15:36:30 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Leor Zolman <le**@bdsoft.com> 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? :-)

Jul 22 '05 #11

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<dt********************@bgtnsc04-news.ops.worldnet.att.net>...
"x" <ao****@hotmail.com> wrote in message
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
Jul 22 '05 #12

P: n/a
x wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #13

P: n/a
Rolf Magnus <ra******@t-online.de> spoke thus:
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 ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #14

P: n/a
On Wed, 14 Apr 2004 14:10:49 +0200, Rolf Magnus <ra******@t-online.de>
wrote:
Leor Zolman wrote:
On Tue, 13 Apr 2004 15:36:30 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Leor Zolman <le**@bdsoft.com> 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::ostringstream 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
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #15

P: n/a
On Wed, 14 Apr 2004 14:17:32 GMT, Leor Zolman <le**@bdsoft.com> wrote:

//
// 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

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #16

P: n/a
"Michiel Salters" <Mi*************@logicacmg.com> wrote in message
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.

Jul 22 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.