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. Convert const string& to CByteArray ?

Convert const string& to CByteArray ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
9 Posts 3 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.
  • M Offline
    M Offline
    mmayur
    wrote on last edited by
    #1

    Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M

    CPalliniC F 2 Replies Last reply
    0
    • M mmayur

      Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Try (I didn't test it...)

      CString str = _T("Hi");
      CByteArray arr;
      int iSize = (str.GetLength()+1) * sizeof(TCHAR)
      const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
      for (i = 0; i < iSize; i++)
      {
      arr.Add(pByte[i]);
      }

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        Try (I didn't test it...)

        CString str = _T("Hi");
        CByteArray arr;
        int iSize = (str.GetLength()+1) * sizeof(TCHAR)
        const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
        for (i = 0; i < iSize; i++)
        {
        arr.Add(pByte[i]);
        }

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        M Offline
        M Offline
        mmayur
        wrote on last edited by
        #3

        Thanks for your reply. The code snippet works fine. But the snippet is for convertion of CString to CByteArray. But i'm looking for a const string conversion to CByteArray. Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop. Regards, Mayur M

        CPalliniC 1 Reply Last reply
        0
        • M mmayur

          Thanks for your reply. The code snippet works fine. But the snippet is for convertion of CString to CByteArray. But i'm looking for a const string conversion to CByteArray. Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop. Regards, Mayur M

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          mayur8u wrote:

          But i'm looking for a const string conversion to CByteArray.

          Are you talking about std::string?

          mayur8u wrote:

          Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop.

          Yes, you may use CByteArray methods SetSize and Getdata to copy the string content into the array buffer.

          CString str = _T("Hi");
          CByteArray arr;
          int iSize = (str.GetLength()+1) * sizeof(TCHAR)
          arr.SetSize(iSize);
          const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
          CopyMemory( arr.GetData(), pByte, iSize);

          As usual, the test is up to you. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          M 1 Reply Last reply
          0
          • CPalliniC CPallini

            mayur8u wrote:

            But i'm looking for a const string conversion to CByteArray.

            Are you talking about std::string?

            mayur8u wrote:

            Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop.

            Yes, you may use CByteArray methods SetSize and Getdata to copy the string content into the array buffer.

            CString str = _T("Hi");
            CByteArray arr;
            int iSize = (str.GetLength()+1) * sizeof(TCHAR)
            arr.SetSize(iSize);
            const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
            CopyMemory( arr.GetData(), pByte, iSize);

            As usual, the test is up to you. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            M Offline
            M Offline
            mmayur
            wrote on last edited by
            #5

            Ya! i'm talking about converting from std::string to CByteArray. If there is no solution then i might have to go to CString. (Last option)

            CPalliniC 1 Reply Last reply
            0
            • M mmayur

              Ya! i'm talking about converting from std::string to CByteArray. If there is no solution then i might have to go to CString. (Last option)

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              std::string str("Hi");
              int iLen = str.length();
              CByteArray arr;
              arr.SetSize(iLen);
              CopyMemory( arr.GetData(), str.c_str(), iLen);

              Please note, the above code is not adding the null terminator at the tail of the array. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              M 1 Reply Last reply
              0
              • CPalliniC CPallini

                std::string str("Hi");
                int iLen = str.length();
                CByteArray arr;
                arr.SetSize(iLen);
                CopyMemory( arr.GetData(), str.c_str(), iLen);

                Please note, the above code is not adding the null terminator at the tail of the array. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                M Offline
                M Offline
                mmayur
                wrote on last edited by
                #7

                How about the CByteArray to std:string?

                M 1 Reply Last reply
                0
                • M mmayur

                  Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M

                  F Offline
                  F Offline
                  frx96
                  wrote on last edited by
                  #8

                  char str[] = "abcdef"; int iSize = strlen(str); BYTE * pByte = (BYTE *) str; CByteArray arr; for (int i = 0; i < iSize; i++) { arr.Add(pByte[i]); } the loop is must use.

                  1 Reply Last reply
                  0
                  • M mmayur

                    How about the CByteArray to std:string?

                    M Offline
                    M Offline
                    mmayur
                    wrote on last edited by
                    #9

                    Hi, I'm able to convert const string& to CByteArray using the memcpy(byteArr.GetData(), source.c_str(), source.length()); Does converting back the CByteArray to const string& work the same way using memcpy? Regards, Mayur M

                    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