-
November 16th, 2005, 07:01 PM
#1
CString and Insertion Operator ( << )
Hi All,
I'm using a library (Crypto++) which defines an Integer (arbitrary size). The Integer class defines operator<<.
I find that I have to continually copy and paste conversion code to display the Integer as a string.
Is there an easy way to perform the following?
Code:
CString s;
CryptoPP::Integer n;
s << n;
Thanks,
Jeff
-
November 16th, 2005, 08:17 PM
#2
Re: CString and Insertion Operator ( << )
How about creating a utility function, e.g. CString operator<<(CString, int)? You just have to include the header to use it in the future.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
November 17th, 2005, 04:46 AM
#3
Re: CString and Insertion Operator ( << )
Are you wedded to clunky old CString? It's possible using standard strings:
Code:
std::ostringstream os;
os << i;
std::string s = os.str();
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 17th, 2005, 06:14 AM
#4
Re: CString and Insertion Operator ( << )
Is there an easy way to perform the following?
Well, that is very easy.
When there is duplicate code somewhere... there are probably functions (or classes) to write.
And functions can be written since Fortran exists.
So, just write a std::string ToString(const CryptoPP::Integer& number) function.
Last edited by SuperKoko; November 17th, 2005 at 06:17 AM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
November 17th, 2005, 03:54 PM
#5
Re: CString and Insertion Operator ( << )
Hi Graham,
Are you wedded to clunky old CString?
Basically - I do a lot with MFC.
Below is probably more helpful...
Code:
CString s;
CryptoPP::Integer n;
s << n;
SetEditWindowText( IDC_EDIT_MODULUS, n );
Then,
Code:
VOID SetEditWindowText( UINT nID, const CString& szText ) {
CEdit* pEdit = static_cast<CEdit*>( GetDlgItem( nID ) );
if( NULL != pEdit ) { pEdit->SetWindowText( szText ); }
}
Jeff
-
November 17th, 2005, 05:00 PM
#6
Re: CString and Insertion Operator ( << )
The last time I used MFC, I basically used std::string/wstring throughout and made use of the c_str() function and the CString constructor to pass the string when it was needed.
Otherwise, you can do it by writing the appropriate operator:
Code:
CString& operator<<(CString&, const CryptoPP::Integer&);
By returning the input CString as the function value, you will be able to chain them together. Of course, that means that you'll probably also want to implement other overloads, like operator<<(CString&, const char*) and so on.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 17th, 2005, 05:43 PM
#7
Re: CString and Insertion Operator ( << )
To answer the original question:
Is there an easy way to perform the following?
Code:
CString s;
CryptoPP::Integer n;
s << n;
Just use the method Format:
Code:
CString s;
CryptoPP::Integer n;
s.Format("%d", n);
Now, probably this isn't exactly correct. I don't know that CryptoPP::Integer is, but I suppose it's a custom class. If it has an operator int() or a ToInt() method, your code should actually look like this:
Code:
s.Format("%d", (int)n);
or
Code:
s.Format("%d", n.ToInt());
or something like that. I hope you got the idea.
-
November 18th, 2005, 07:25 AM
#8
Re: CString and Insertion Operator ( << )
Cilu : Since CryptoPP::Integer can have an arbitrary size, I suppose that jeffrey wants to get all the digits of the integer, and not the number modulo 2^32.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
November 18th, 2005, 05:12 PM
#9
Re: CString and Insertion Operator ( << )
Hi All,
Thanks for the responses. I tossed out points where the system would let me (SuperKoko: sorry - I'm told I have to spread them around).
In the end, I see I left out an important detail: CryptoPP::Integer defines operator<< on a stream (so pumping to std::cout works as expected).
I guess in the end I was looking for conversion code from the stream to a char[], and then from a char[] to a CString (trivial).
Jeff
-
November 18th, 2005, 05:25 PM
#10
Re: CString and Insertion Operator ( << )
See my post at http://www.codeguru.com/forum/showthread.php?t=365308
for possible operator << and operator >> for CString. It also
has a conversion function. You could use like this:
Code:
CString str = Convert<CryptoPP::Integer,CString>(n);
-
November 18th, 2005, 06:46 PM
#11
Re: CString and Insertion Operator ( << )
Originally Posted by jeffrey@toad.net
Hi All,
Thanks for the responses. I tossed out points where the system would let me (SuperKoko: sorry - I'm told I have to spread them around).
In the end, I see I left out an important detail: CryptoPP::Integer defines operator<< on a stream (so pumping to std::cout works as expected).
I guess in the end I was looking for conversion code from the stream to a char[], and then from a char[] to a CString (trivial).
Jeff
So you just stream it to an ostringstream and use the str() function to initialise the CString
Code:
ostringstream os;
os << integer;
CString cs(os.str());
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|