Convert C ++ code (with pointers and bit shift) to C #

I am currently involved in a project to port C ++ code to C #, but some fragments are not so easy to port from 1 to 1, for example:

#define CMND_MOVEL              10
#define CMND_EXTRASIDE          0x80

char CmndBuffer[32];

int *dst;
dst = (int*) &CmndBuffer[0];

*dst = 0;
*dst |= (CMND_MOVEL + (Flags << 8));      
if (BoxSide) *dst |= CMND_EXTRASIDE;

dst++;
*dst = SequenceNr;
dst++;
*dst = 10 * LastDestination.x;
dst++;
*dst = 10 * LastDestination.y;
dst++;
*dst = 10 * LastDestination.z;
dst++;
*dst = Speed * 10;
dst++;
*dst = Accel * 10;    

result = ERR_COMMSOCK;
if (UdpCmdSocket >= 0)
{
    if (sendto(UdpCmdSocket, (const char*) CmndBuffer, 28, 0, (struct sockaddr*)&UdpCmdPeer, sizeof(UdpCmdPeer)) != SOCKET_ERROR)
    {
        // more logic here
    }
}

Can someone explain to me in detail what is happening here? I know well how pointers and bit shifting work, but I'm not 100% sure what happens here at the byte level. I see that it fills the data packet to pass it on top of UDP.

But much more important: how to send this to C #? (I will use the .NET Socket class)

I know this might be conceived as a lazy question, but I thought maybe SO could help me with that.

thanks

+4
source share
3 answers

, , , sizeof(int) 4, . , CMND_MOVEL CMD_EXTRASIDE, 3 - Flags. :

-------------------------------------------------------------
| Flags<<8      | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| CMD_MOVEL     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
| CMD_EXTRASIDE | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-------------------------------------------------------------

char C/++ , byte #. , , int* 4 .

:

//CmndBuffer[0] to zero
*dst = 0;

0 CmndBuffer[0] CmdBuffer[3]. dst , CmdBuffer[27]. , .

, - . - :

byte[] buffer = /*...*/;
int index = /*...*/;
int sequenceNr = /*...*/;

buffer[index] = (byte) (sequenceNr >> 24) & 0xFF;
buffer[++index] = (byte) (sequenceNr >> 16) & 0xFF;
buffer[++index] = (byte) (sequenceNr >> 8) & 0xFF;
buffer[++index] = (byte) sequenceNr & 0xFF;
+2

//some constants for later use
#define CMND_MOVEL              10
#define CMND_EXTRASIDE          0x80

char CmndBuffer[32];

int *dst;
//Load the address of the first element of CmndBuffer into dst;
dst = (int*) &CmndBuffer[0];

//CmndBuffer[0] to zero
*dst = 0;
//this loads (CMND_MOVEL + (Flags << 8) into dst. Flags << 8 means a multiplication with 2^8
*dst |= (CMND_MOVEL + (Flags << 8));      
if (BoxSide) *dst |= CMND_EXTRASIDE;

//go to the next array element. The same applies to the commands below
dst++;
//write the value into the current array element
*dst = SequenceNr;
dst++;
*dst = 10 * LastDestination.x;
dst++;
*dst = 10 * LastDestination.y;
dst++;
*dst = 10 * LastDestination.z;
dst++;
*dst = Speed * 10;
dst++;
*dst = Accel * 10;    

result = ERR_COMMSOCK;
if (UdpCmdSocket >= 0)
{
    if (sendto(UdpCmdSocket, (const char*) CmndBuffer, 28, 0, (struct sockaddr*)&UdpCmdPeer, sizeof(UdpCmdPeer)) != SOCKET_ERROR)
    {
        // more logic here
    }
}

this

+2
Lucas has already covered most of it; but what it effectively does is move through the array using pointer math. So:
    dst++;
    *dst = 10 * LastDestination.x;

in C # will look like this:

    var arrayIndex = 0;
    CmndBuffer[arrayIndex++] = 10 * LastDestination.x;
    CmndBuffer[arrayIndex++] = 10 * LastDestination.y;

etc.

+1
source

All Articles