-1

I am looking for a method that will do the opposite of memcpy when I make a copy of a buffer. For example, if I had this code

 _memccpy(szbuffer, buffer, BUFFER_SIZE, 0);

The result of szbuffer is then binary. If I send this over from a socket to a server what can I use to convert the result of szbuffer back to text format instead of binary. I know that I can simply send it over as text but that is not an option for my assignment. Any suggestions?

VMA92
  • 469
  • 2
  • 8
  • 19
  • 4
    This just copies bytes from one place in memory to another. What those bytes are doesn't matter and no conversion is taking place. – Steve Sep 26 '14 at 20:04
  • @Vince Abruzzese: memcpy won´t do any conversions. If the result is different, you hve something wrong in your program. – deviantfan Sep 26 '14 at 20:05
  • 2
    The `memcpy` function copies bytes, regard less of binary or text. The opposite would be to not copy. Why would you need a function to *not copy" data? – Thomas Matthews Sep 26 '14 at 20:07
  • @deviantfan According to this link http://www.cplusplus.com/reference/cstring/memcpy/ it says the result will be binary. – VMA92 Sep 26 '14 at 20:07
  • @ThomasMatthews I was using the memcpy to send it to binary and then once binary I send it over to the server but once its on the server I have a binary result instead of text – VMA92 Sep 26 '14 at 20:09
  • @Vince Abruzzese: That sentence means that pointer destinations etc. arent copied together with the address and things like that. Text is binary too in the computer, don´t worry about that – deviantfan Sep 26 '14 at 20:09
  • It says "binary copy", in contrast to value-copy. Some types (esp. in C++) have more complex copy-semantics, and a binary copy is useless/a really bad idea. – Deduplicator Sep 26 '14 at 20:09
  • @deviantfan so if im sending it to the server as an array of chars it is considered binary? – VMA92 Sep 26 '14 at 20:11
  • @VinceAbruzzese It's composed of 8-bit bytes, isn't it? – Steve Sep 26 '14 at 20:12
  • @Steve Bytes being (or not being) 8-Bit has nothing to do with the question at hand. – Deduplicator Sep 26 '14 at 20:13
  • Ok... "The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function". That's probably relevant to the topic then :) – Steve Sep 26 '14 at 20:14
  • @VinceAbruzzese: Yes, because any data in your computer is binary. Don´t let Windows text-file-mode confuse you. – deviantfan Sep 26 '14 at 20:14
  • @deviantfan Ok in that case I won't use memcpy. Thanks for clearing that up – VMA92 Sep 26 '14 at 20:16

1 Answers1

0

Don't use memcpy for conversions. The memcpy copies data bit for bit from one location to another.

You want something like snprintf or std::ostringstream. These functions handle data representations from internal to textual.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154