473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
16 1920
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
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
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

"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

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

"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
"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
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
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
"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.