Re: converting 1944 to '1','9','4','4'
From: Leor Zolman (leor_at_bdsoft.com)
Date: 04/14/04
- Next message: hall: "Strange abortion of for loop"
- Previous message: Thomas Matthews: "Re: Problems with multiplications of doubles and/or floats"
- In reply to: Rolf Magnus: "Re: converting 1944 to '1','9','4','4'"
- Next in thread: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Reply: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 14 Apr 2004 14:17:32 GMT
On Wed, 14 Apr 2004 14:10:49 +0200, Rolf Magnus <ramagnus@t-online.de>
wrote:
>Leor Zolman wrote:
>
>> On Tue, 13 Apr 2004 15:36:30 +0000 (UTC), Christopher Benson-Manica
>> <ataru@nospam.cyberspace.org> wrote:
>>
>>>Leor Zolman <leor@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
- Next message: hall: "Strange abortion of for loop"
- Previous message: Thomas Matthews: "Re: Problems with multiplications of doubles and/or floats"
- In reply to: Rolf Magnus: "Re: converting 1944 to '1','9','4','4'"
- Next in thread: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Reply: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]