1

I'm trying to send a BYTE* over a socket, but the send function only allows me to send a char* buffer. Is this possible? How would I go about casting it back on the other side?

Nathelol
  • 577
  • 7
  • 25
  • 1
    Have you tried casting?: `send(socket, reinterpret_cast(byte), sizeof(byte), flag)`. – David G Dec 16 '14 at 21:01
  • @0x499602D2: in your example, assuming `byte` is a `BYTE*`, thus `sizeof(byte)` is always 4 on 32bit systems, and 8 on 64bit systems. `sizeof()` would only work if `byte` is a fixed array instead. – Remy Lebeau Dec 16 '14 at 21:10
  • @RemyLebeau Typo. Should be `sizeof(BYTE)`, right? – David G Dec 16 '14 at 21:12
  • @0x499602D2: No, that's even worse, because `sizeof(BYTE)` is always 1. So unless you are actually sending 1 byte at a time, you need to specify the real number of bytes that are being pointed at in the second parameter. – Remy Lebeau Dec 16 '14 at 21:16

1 Answers1

4

Use reinterpret_cast to cast from BYTE* to char*. A BYTE is an unsigned char typedef, so you shouldn't have any issues.

char* foo = reinterpret_cast<char*>(bar);

Where bar is your BYTE*.

Lander
  • 3,369
  • 2
  • 37
  • 53
  • Thanks yeah that's what I've been doing I've actually figured out that the problem may not be due to the cast. When I cast it the char* only seems to have 9 characters rather than the 1000 there should be! Any ideas on this? – Nathelol Dec 16 '14 at 21:12
  • 1
    @Nathelol: That implies that you are passing the wrong value in the third parameter, such as if you are using `strlen()` on binary data, for instance. – Remy Lebeau Dec 16 '14 at 21:18
  • 1
    It would look that way if the 10th byte is a zero. But that is just a debugger viewing characteristic. – ScottMcP-MVP Dec 16 '14 at 21:18
  • the BYTE* is a pointer to byte[0] in byte[3][1000] so when it's cast with reinterpret_cast(buffers[0]); it only takes the first 9 as in byte[0][0], byte[0][1]... etc – Nathelol Dec 16 '14 at 21:21