473,320 Members | 1,876 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,320 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 1917
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.