Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Copy a CString to a Stream*

Copy a CString to a Stream*

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
5 Posts 3 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #1

    Hi! I am using an application that is using both managed to unmanaged code. I want to copy data from a CString to a Stream* (it may be MemoryStream or BufferedStream - whichever is faster). Can anyone help? Thanks Best regards, Alexandru Savescu

    N 1 Reply Last reply
    0
    • A Alexandru Savescu

      Hi! I am using an application that is using both managed to unmanaged code. I want to copy data from a CString to a Stream* (it may be MemoryStream or BufferedStream - whichever is faster). Can anyone help? Thanks Best regards, Alexandru Savescu

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Convert the CString to a String* and then pass the String* to the Stream* Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      A 1 Reply Last reply
      0
      • N Nish Nishant

        Convert the CString to a String* and then pass the String* to the Stream* Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        A Offline
        A Offline
        Alexandru Savescu
        wrote on last edited by
        #3

        Sorry Nish, it does not work. Here is what I did:

        CString str = "aaaa":
        String\* str1 = new String (str);
        MemoryStream\* mem1 = new MemoryStream (str1, false);
        

        and the result is:

        c:\Work\Test.cpp(332): error C2664: 'System::IO::MemoryStream::MemoryStream(unsigned char __gc[],bool)' : cannot convert parameter 1 from 'System::String __gc *' to 'unsigned char __gc[]'

        I found an work arround that compiles and works but I hate it:

        MemoryStream* mem = new MemoryStream (n) ;// specify the size
        for (int i = 0; i < n; i++)
        mem->WriteByte (str[i]);

        Do you have another solution? Thanks! Best regards, Alexandru Savescu

        M 1 Reply Last reply
        0
        • A Alexandru Savescu

          Sorry Nish, it does not work. Here is what I did:

          CString str = "aaaa":
          String\* str1 = new String (str);
          MemoryStream\* mem1 = new MemoryStream (str1, false);
          

          and the result is:

          c:\Work\Test.cpp(332): error C2664: 'System::IO::MemoryStream::MemoryStream(unsigned char __gc[],bool)' : cannot convert parameter 1 from 'System::String __gc *' to 'unsigned char __gc[]'

          I found an work arround that compiles and works but I hate it:

          MemoryStream* mem = new MemoryStream (n) ;// specify the size
          for (int i = 0; i < n; i++)
          mem->WriteByte (str[i]);

          Do you have another solution? Thanks! Best regards, Alexandru Savescu

          M Offline
          M Offline
          Michael Mac
          wrote on last edited by
          #4

          Try this:

          CString str = "aaaa";
          String* strText = new String (str);
          Byte message[] = System::Text::Encoding::ASCII->GetBytes( strText );
          MemoryStream* sStream = new MemoryStream( message );

          If you want to use diffrent encoding replace the bold text with the appropriate encoding like: Unicode, UTF7 or UTF8. I should work :)


          43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

          A 1 Reply Last reply
          0
          • M Michael Mac

            Try this:

            CString str = "aaaa";
            String* strText = new String (str);
            Byte message[] = System::Text::Encoding::ASCII->GetBytes( strText );
            MemoryStream* sStream = new MemoryStream( message );

            If you want to use diffrent encoding replace the bold text with the appropriate encoding like: Unicode, UTF7 or UTF8. I should work :)


            43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #5

            Thanks. This really looks like a solution. Best regards, Alexandru Savescu

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups