-5

I am currently learning c ++ and i a beginner. Since not too strict to me: D

I programmed a winsocket udp client in c ++. This is now send bytes array (like 54-53-33-49-4E-49-54-31-00-65-00-00-88-06-3B-EC-E9-00-5A-6D-F3-12-C9-B6-6E-82-00-00-00-00-00-00-00-00) to a remote udp server. And this is my problem. I build the array in C # like that

byte[] sendData= new byte[versionLen + initTypeLen + 4 + 4 + 8];
byte[] Initversion = { 0x06, 0x3b, 0xec, 0xe9 };
Array.Copy(Initversion, 0, sendData, 0, versionLen);
[...]

Now in C ++ I have very strong problems. First I understood correctly that I have to build this in chars []?

My try:

char init1[/*VersionLength + InitPackLength + TimestampLength + 4 + 8*/ 9];
char Initversion[] = { 0x06, 0x3b, 0xec, 0xe9 };
char StepNumber[] = { 0x00 };

init1[0] = 0x06;
init1[1] = 0x3b;
init1[2] = 0xec;
init1[3] = 0xe9;
init1[4] = 0x00;
init1[5] = 0x06;
init1[6] = 0x06;
init1[7] = 0x06;
init1[8] = 0x06;

But the result: 06-3B-EC-E9 C++ remove all bytes after the 0x00. How can i fix that? What is the right solution?

Thank you.

EDIT: Thats my code: https://paste.ee/p/vfYf4

VerHext
  • 23
  • 2
  • 7
  • 4
    Use a `std::vector` instead of a `char[]` array. To add values use the `push_back()` function. –  Jan 28 '18 at 16:53
  • 1
    What code is actually copying data into `init1`? – Joe Jan 28 '18 at 16:53
  • `But the result: 06-3B-EC-E9` that is because the next byte is `0x00` and if you print it out as a string it is interpreted as the string terminator. Do not print it out as a string. – Killzone Kid Jan 28 '18 at 17:16
  • @TheDude now i change the type to std::vector. But the problem isn't fixed. – VerHext Jan 28 '18 at 17:25
  • @KillzoneKid that is not right, the result string is from the remote udp server. And the client in c++ send only 4 bytes. – VerHext Jan 28 '18 at 17:27
  • @VerHext You need to show more code including how you copy and send it – Killzone Kid Jan 28 '18 at 17:36
  • @KillzoneKid thats my code: https://paste.ee/p/vfYf4 – VerHext Jan 28 '18 at 17:44
  • @VerHext `strlen(reinterpret_cast(buff.data()))` is your problem, I bet it returns 4, because as I mentioned before, it counts `0x00` as string terminator. Pass the actual length of the packet in bytes there. – Killzone Kid Jan 28 '18 at 17:46
  • @KillzoneKid Thank you :D. That was my problem. – VerHext Jan 28 '18 at 17:52

1 Answers1

0

In C and C++, regular character strings are terminated by a '\0' character. So it appears you are using a string function to copy the data. That is, it stops copying once it hits the '\0' value.

Interestingly, you left the part that copies the data (and maybe just the part the displays the data) out of your question. So we can't tell you which function that is. But we can see that's what's happening.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466