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. strcat_memcpy

strcat_memcpy

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmindata-structuresquestion
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.
  • K Offline
    K Offline
    khushboo gupta
    wrote on last edited by
    #1

    I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.

    P L 2 Replies Last reply
    0
    • K khushboo gupta

      I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      If you are sending pure binary data then forget about functions that treat byte sequences as zero terminated strings. Don't use strcat but memcpy should be okay to move bytes here and there if you use it correctly. Along with your binary data you should store the length/size of the data in an int or size_t variable. If you are using C++ you could simply use an std::vector for this job.

      class CStorage
      {
      size_t m_Size;
      char m_Bytes[100];

      public:
      CStorage()
      : m_Size(0)
      {}
      void Append(const char* data, size_t size)
      {
      memcpy(m_Bytes+m_Size, data, size);
      }
      };

      OR

      #include <vector>
      #include <iterator>

      static const char appendable[] = "abcdefgh";
      std::vector<char> bytes;
      std::copy(appendable, appendable+sizeof(appendable)/sizeof(appendable[0]), std::back_inserter(bytes));

      F 1 Reply Last reply
      0
      • K khushboo gupta

        I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I explained yesterday[^] that you cannot use the string functions for binary data that contains null characters: you must use memcpy(). If the server is receiving 3030 rather than 00, then it means your sending code is not converting it from characters to binary.

        Use the best guess

        1 Reply Last reply
        0
        • P pasztorpisti

          If you are sending pure binary data then forget about functions that treat byte sequences as zero terminated strings. Don't use strcat but memcpy should be okay to move bytes here and there if you use it correctly. Along with your binary data you should store the length/size of the data in an int or size_t variable. If you are using C++ you could simply use an std::vector for this job.

          class CStorage
          {
          size_t m_Size;
          char m_Bytes[100];

          public:
          CStorage()
          : m_Size(0)
          {}
          void Append(const char* data, size_t size)
          {
          memcpy(m_Bytes+m_Size, data, size);
          }
          };

          OR

          #include <vector>
          #include <iterator>

          static const char appendable[] = "abcdefgh";
          std::vector<char> bytes;
          std::copy(appendable, appendable+sizeof(appendable)/sizeof(appendable[0]), std::back_inserter(bytes));

          F Offline
          F Offline
          Freak30
          wrote on last edited by
          #4

          You CStorage::Append() should do a m_Size += size; after the memcpy(). :)

          P 1 Reply Last reply
          0
          • F Freak30

            You CStorage::Append() should do a m_Size += size; after the memcpy(). :)

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #5

            Of course. :-)

            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