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. CString to CByteArray

CString to CByteArray

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiodebugginghelpquestion
5 Posts 4 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:

    CByteArray arrByte2;
    BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
    

    // sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
    // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
    arrByte2.SetSize(sSerial.GetLength());
    memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
    // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
    // arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««

    these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.

    J V L 3 Replies Last reply
    0
    • _ _Flaviu

      Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:

      CByteArray arrByte2;
      BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
      

      // sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
      // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
      arrByte2.SetSize(sSerial.GetLength());
      memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
      // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
      // arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««

      these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      It looks like the VS debugger is dumping out additional (or the allocated) memory. Is it for exactly the posted code? Or is arrByte2 declared somewhere else and has been used before? You are also copying m_sSerial.GetLength() bytes while the array size has been set to sSerial.GetLength(). If m_sSerial is a longer string, you will have a buffer overrun. Finally, your casting should be

      const BYTE* pByteArray = reinterpret_cast<const BYTE*>(sSerial.GetString());

      That will at least keep the constness of the string but fail too for Unicode builds when sSerial is not explicitly declared as CStringA. You might call arrByte2.GetSize() and ignore the additional output if that is as expected.

      1 Reply Last reply
      0
      • _ _Flaviu

        Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:

        CByteArray arrByte2;
        BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
        

        // sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
        // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
        arrByte2.SetSize(sSerial.GetLength());
        memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
        // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
        // arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««

        these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Flaviu2 wrote:

        Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.

        It does NOT have this trash "on tail". This trash is behind the arrByte2. Debugger interprets its content as characters and cannot find the actual "end" of array since you did not copy the terminated NULL from the CString.

        1 Reply Last reply
        0
        • _ _Flaviu

          Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:

          CByteArray arrByte2;
          BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
          

          // sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
          // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
          arrByte2.SetSize(sSerial.GetLength());
          memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
          // pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
          // arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««

          these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          You need to copy the length of the string + 1 to get the ending '\0' to terminate a C string :-) Very basic .. without it the string keeps going in C terms which is exactly what the debugger is showing with rubbish that was in the buffer.

          In vino veritas

          _ 1 Reply Last reply
          0
          • L leon de boer

            You need to copy the length of the string + 1 to get the ending '\0' to terminate a C string :-) Very basic .. without it the string keeps going in C terms which is exactly what the debugger is showing with rubbish that was in the buffer.

            In vino veritas

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            Thank you all of you. Yes, that was the problem, I should put

            arrByte2.SetSize(sSerial.GetLength() + 1);

            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