++ , IBM-PC, # -. , .
#, , 1 , # little-endian ++. WORD (16 ) DWORD (32 )
#define WORD_SWAP(x) ((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00))
#define DWORD_SWAP(x) ((((x) >> 24) & 0x000000ff) | \
(((x) >> 8) & 0x0000ff00) | \
(((x) << 8) & 0x00ff0000) | \
(((x) << 24) & 0xff000000))
#. .
PS: , .
Endianness,
#
ushort Swap(ushort x)
{
return (ushort)((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00));
}
uint Swap(uint x)
{
return ((((x) >> 24) & 0x000000ff) |
(((x) >> 8) & 0x0000ff00) |
(((x) << 8) & 0x00ff0000) |
(((x) << 24) & 0xff000000));
}
# client UDPClient . .
struct mdata
{
UInt32 mark_kupnr;
UInt16 mark_provnr;
UInt16 markriktning;
UInt16 xpos;
UInt16 ypos;
}
// example of mdata read with sockets
mbata data;
var bytes = client.Receive()
data.mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
data.mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
data.markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
data.xpos = Swap(BitConverter.ToUInt16(bytes, 8));
data.ypos = Swap(BitConverter.ToUInt16(bytes, 10));
, UInt32 UInt16 . List<byte>.AddRange(variable_byte_array);, UDPClient.Send(formed_array);.
++ [0][1][2][3][4][5][6][7][8][9].
mdata 10 0 9.
typedef struct
{
UInt32 kupnr;
UInt16 lngd;
UInt16 bredd;
UInt16 tjocklek;
sbyte slagkraft;
byte antal;
struct mdata mark[10];
} markdata;
. , () . ()
mdatap >
void Receive(UDPClient client)
{
var bytes = client.Receive()
mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
xpos = Swap(BitConverter.ToUInt16(bytes, 8));
ypos = Swap(BitConverter.ToUInt16(bytes, 10));
}
void Send(UDPClient client)
{
var listOfBytes = new List<byte>();
listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_kupnr)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_provnr)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(markriktning)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(xpos)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(ypos)));
client.Send(listOfBytes.ToArray(), listOfBytes.Count);
}
void Receive(byte[] bytes)
{
mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
xpos = Swap(BitConverter.ToUInt16(bytes, 8));
ypos = Swap(BitConverter.ToUInt16(bytes, 10));
}
void Send(out byte[] bytes)
{
var listOfBytes = new List<byte>();
listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_kupnr)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_provnr)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(markriktning)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(xpos)));
listOfBytes.AddRange(BitConverter.GetBytes(Swap(ypos)));
bytes = listOfBytes.ToArray();
}
markdata, , , . .
mdata.