CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2005
    Location
    Pasadena, MD, USA
    Posts
    105

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  5. #5
    Join Date
    Feb 2005
    Location
    Pasadena, MD, USA
    Posts
    105

    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

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  9. #9
    Join Date
    Feb 2005
    Location
    Pasadena, MD, USA
    Posts
    105

    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

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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);

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: CString and Insertion Operator ( << )

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured