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
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.
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();
Re: CString and Insertion Operator ( << )
Quote:
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. :D
So, just write a std::string ToString(const CryptoPP::Integer& number) function.
Re: CString and Insertion Operator ( << )
Hi Graham,
Quote:
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
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.
Re: CString and Insertion Operator ( << )
To answer the original question:
Quote:
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.
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.
;)
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
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);
Re: CString and Insertion Operator ( << )
Quote:
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());