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. C / C++ / MFC
  4. Copying from unsigned char * to the CString

Copying from unsigned char * to the CString

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 5 Posters 0 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
    ashishbhatt 0
    wrote on last edited by
    #1

    Hi All, I have unsigned char * buffer in which some data are stored , now I want to move these all data to my CString variable for some operations. I tried to copy but problem is that in my unsigned char * some data are padded as zero, So I hope that in CString it gets the null value so it can not copy whole data. I tried to copy from unsigned char * to the CString using... CString tmpMsg = CString(msg); // Here msg is the unsigned char *

    Ashish Bhatt

    J N J 3 Replies Last reply
    0
    • A ashishbhatt 0

      Hi All, I have unsigned char * buffer in which some data are stored , now I want to move these all data to my CString variable for some operations. I tried to copy but problem is that in my unsigned char * some data are padded as zero, So I hope that in CString it gets the null value so it can not copy whole data. I tried to copy from unsigned char * to the CString using... CString tmpMsg = CString(msg); // Here msg is the unsigned char *

      Ashish Bhatt

      J Offline
      J Offline
      john5632
      wrote on last edited by
      #2

      try memcpy()

      A 1 Reply Last reply
      0
      • A ashishbhatt 0

        Hi All, I have unsigned char * buffer in which some data are stored , now I want to move these all data to my CString variable for some operations. I tried to copy but problem is that in my unsigned char * some data are padded as zero, So I hope that in CString it gets the null value so it can not copy whole data. I tried to copy from unsigned char * to the CString using... CString tmpMsg = CString(msg); // Here msg is the unsigned char *

        Ashish Bhatt

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        Actually you just need to keep the data in CString? Then use GetBuffer, and copy the data using CopyMemory. But remember, you should not perform any string operations because it is having null characters...

        - NS -

        1 Reply Last reply
        0
        • J john5632

          try memcpy()

          A Offline
          A Offline
          ashishbhatt 0
          wrote on last edited by
          #4

          Thanks ... I used memcpy() as u told me, But I used like ........ memcpy(&tempMsg,(unsigned char*)msg,noOfBytes); Then it gives me the run time Access violation error as below... TestRTMP.exe: 0xC0000005: Access violation reading location 0x49468964. So,plz help me , is there any other way to copying.

          Ashish Bhatt

          T 1 Reply Last reply
          0
          • A ashishbhatt 0

            Thanks ... I used memcpy() as u told me, But I used like ........ memcpy(&tempMsg,(unsigned char*)msg,noOfBytes); Then it gives me the run time Access violation error as below... TestRTMP.exe: 0xC0000005: Access violation reading location 0x49468964. So,plz help me , is there any other way to copying.

            Ashish Bhatt

            T Offline
            T Offline
            Tydia kun
            wrote on last edited by
            #5

            Does not surprise me that it gives you an access violation when doing that. The first and foremost rule to remember is: DO NOT USE MEMCPY ON CLASSES. Classes work independently, managing their own memory and so on. Therefore, you should use the exposed interfaces in the classes to do stuff. In this case, you're overwriting important data in the class, not to mention that the class's internal buffer is nowhere large enough to hold your data. As mentioned, use CString.GetBuffer(LengthOfBuffer). It returns a pointer to a buffer. Copy data using memcpy/CopyMemory. Then call CString.ReleaseBuffer(LengthOfData). That should do the trick.

            1 Reply Last reply
            0
            • A ashishbhatt 0

              Hi All, I have unsigned char * buffer in which some data are stored , now I want to move these all data to my CString variable for some operations. I tried to copy but problem is that in my unsigned char * some data are padded as zero, So I hope that in CString it gets the null value so it can not copy whole data. I tried to copy from unsigned char * to the CString using... CString tmpMsg = CString(msg); // Here msg is the unsigned char *

              Ashish Bhatt

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #6

              The CString class is designed to handle C-style strings, meaning strings that are terminated with a character value of zero (AKA NUL).  It is not designed to handle raw data that contains embedded NUL characters, not double-NUL-terminated collections of strings.    Sounds like you may be a bit too comfortable with CStrings... :)

              -=- James
              Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              See DeleteFXPFiles

              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