ByteArray - Length - Send - Receive - Endian

M

Martin Greul

http://www1.minpic.de/bild_anzeigen.php?id=80889&key=62441166&ende

Hello,

looks well for send the data.

But how can I receive the data and then I need the length.
How is the correct analysis?

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];

bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

// How I get now the length?
}

Greeting Martin

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The whole test project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace WA_ByteArray_01
{
class Endian
{
public static short Swap(short v)
{
return (short)(((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00));
}


public static int Swap(int v)
{
uint v2 = (uint)v;
return (int)(((v2 >> 24) & 0x000000FF) |
((v2 >> 8) & 0x0000FF00) |
((v2 << 8) & 0x00FF0000) |
((v2 << 24) & 0xFF000000));
}

public static void Test_Send()
{
Byte[] bytesSend = new Byte[1000];

int i = 365;
// write in big-endian order, regardless of host order
bytesSend[0] = (Byte)(i >> 24);
bytesSend[1] = (Byte)(i >> 16);
bytesSend[2] = (Byte)(i >> 8);
bytesSend[3] = (Byte)i;

}

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];

bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

// How I get now the length?
}

public static void Test_Send_ByteArrayAsReference(ref Byte[]
bytesSend)
{

int i = 365;
// write in big-endian order, regardless of host order
bytesSend[0] = (Byte)(i >> 24);
bytesSend[1] = (Byte)(i >> 16);
bytesSend[2] = (Byte)(i >> 8);
bytesSend[3] = (Byte)i;

}

}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WA_ByteArray_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Endian.Test_Send();
Endian.Test_Receive();

Byte[] bytesSend = new Byte[1000];

Endian.Test_Send_ByteArrayAsReference(ref bytesSend);

}
}
}
 
P

Peter Duniho

http://www1.minpic.de/bild_anzeigen.php?id=80889&key=62441166&ende

Hello,

looks well for send the data.

But how can I receive the data and then I need the length.
How is the correct analysis?

This is basically the exact inverse of the previous question you asked.
If you managed to get a useful answer out of that thread, just go the
other direction with BinaryReader and the appropriate Read() overloads.

If you didn't manage to get a useful answer out of that thread, I don't
see what hope there is of successfully answering this version of the same
question.

Pete
 
B

Ben Voigt [C++ MVP]

Martin Greul said:
http://www1.minpic.de/bild_anzeigen.php?id=80889&key=62441166&ende

Hello,

looks well for send the data.

But how can I receive the data and then I need the length.
How is the correct analysis?

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];

bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

// How I get now the length?

// from big-endian byte array to 32-bit integer
uint len = (((uint)b1) << 24) | (((uint)b2) << 16) | (((uint)b3) << 8) |
(uint)b4;
}

Greeting Martin

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The whole test project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace WA_ByteArray_01
{
class Endian
{
public static short Swap(short v)
{
return (short)(((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00));
}


public static int Swap(int v)
{
uint v2 = (uint)v;
return (int)(((v2 >> 24) & 0x000000FF) |
((v2 >> 8) & 0x0000FF00) |
((v2 << 8) & 0x00FF0000) |
((v2 << 24) & 0xFF000000));
}

public static void Test_Send()
{
Byte[] bytesSend = new Byte[1000];

int i = 365;
// write in big-endian order, regardless of host order
bytesSend[0] = (Byte)(i >> 24);
bytesSend[1] = (Byte)(i >> 16);
bytesSend[2] = (Byte)(i >> 8);
bytesSend[3] = (Byte)i;

}

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];

bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

// How I get now the length?
}

public static void Test_Send_ByteArrayAsReference(ref Byte[]
bytesSend)
{

int i = 365;
// write in big-endian order, regardless of host order
bytesSend[0] = (Byte)(i >> 24);
bytesSend[1] = (Byte)(i >> 16);
bytesSend[2] = (Byte)(i >> 8);
bytesSend[3] = (Byte)i;

}

}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WA_ByteArray_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Endian.Test_Send();
Endian.Test_Receive();

Byte[] bytesSend = new Byte[1000];

Endian.Test_Send_ByteArrayAsReference(ref bytesSend);

}
}
}
 
M

Martin Greul

Hello,
// from big-endian byte array to 32-bit integer
uint len = (((uint)b1) << 24) | (((uint)b2) << 16) | (((uint)b3) << 8) |
(uint)b4;
ok looks good and I think the correct way. Thanks.

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];
bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

i = (int)(Math.Pow(2, 8)) * b3 + b4;
//** Problem casten, or?
}

What you prefer, your solution?
Or my, is possible, or?
i = (int)(Math.Pow(2, 8)) * b3 + b4;

Greeting Martin
 
B

Ben Voigt [C++ MVP]

Martin Greul said:
Hello,
// from big-endian byte array to 32-bit integer
uint len = (((uint)b1) << 24) | (((uint)b2) << 16) | (((uint)b3) << 8) |
(uint)b4;
ok looks good and I think the correct way. Thanks.

public static void Test_Receive()
{
Byte[] bytesReceive = new Byte[1000];
bytesReceive[0] = 0;
bytesReceive[1] = 0;
bytesReceive[2] = 1;
bytesReceive[3] = 109;

int i = 0;
Byte b1 = bytesReceive[0];
Byte b2 = bytesReceive[1];
Byte b3 = bytesReceive[2];
Byte b4 = bytesReceive[3];

i = (int)(Math.Pow(2, 8)) * b3 + b4;
//** Problem casten, or?
}

What you prefer, your solution?
Or my, is possible, or?
i = (int)(Math.Pow(2, 8)) * b3 + b4;

Yuck! Why would you want to use a function call, bring in the FPU for
conversion to floating point, exponentiation, and conversion back to
integer, when you can just use an integer shift which is one of the fastest
instructions available?

A really really good optimizer might turn your code into mine, but mine is
shorter and canonical.

Put yours in a comment if you want. But mine means something (bits 31-24
come from b1, bits 23-16 come from b2, etc) and if you're going to deal with
building datagrams it's good to learn to think the same way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top