Byte building

  • Thread starter Thread starter Gianmaria I.
  • Start date Start date
G

Gianmaria I.

Hi,
having a BitArray, how can i extract bits to create a System.byte

as in the example...

With
BitArray bits
and
Byte myNewByte

bits[0] to bits[7] => myNewByte
 
Gianmaria,

You will want to call the CopyTo method, passing in a byte array with
one element, like so:

// The byte array of one element.
byte[] bytes = new byte[1];

// Call the CopyTo method.
bits.CopyTo(bytes, 0);

Hope this helps.
 
It has been since beta since I did any of this, but here is some old source
code that might help you out. Someone on this same forum had helped me
about 4 years ago with a similar problem when I was working out a protocal
for an online game. If you look at what I am doing there are a couple
choices that might help you accomplish what you need. Pay special attention
to how I calculate the data length from the first 4 bytes of my packet, this
will probably be your most efficient method of doing what you want. Here is
my code.

using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

using System.Diagnostics;

namespace MudClient

{

/// <summary>

/// Summary description for Packet.

/// </summary>

///

public enum PacketType:byte

{

IgnoreThisPacket,

BroadcastToServer,

LoginToServer,

LoginAccepted,

LoginDenied,

LoginReqestfromServer,

QuitToServer,

QuitToClient,

ServerShuttingDown,

SetCommandLineToClient,

CommandToServer,

CommandToClient,

TextToClient,

PingClient,

PongServer,

AbbrTextToClient,

RequestAbbrTextValue,

SendAbbrTextValue,

ClearAbbrTextOnClient

}

public class Packet

{

private byte command;

private int dataLength=0;

private byte[] Data;

public byte[] PacketData

{

get

{

return Data;

}

set

{

Data=value;

dataLength=Data.Length;

}

}

public int Length

{

get

{

if (Data==null)

return 5;

else

return dataLength+5;

}

}

public PacketType Command

{

get

{

return (PacketType)command;

}

set

{

command=(byte)value;

}

}

public string StrPacketData

{

get {return System.Text.Encoding.ASCII.GetString(Data);}

set

{

Data=System.Text.Encoding.ASCII.GetBytes(value.ToCharArray());

dataLength=Data.Length;

}

}

public Packet()

{

Command=PacketType.IgnoreThisPacket;

}

public Packet(PacketType cmd)

{

Command=cmd;

}

public Packet(byte[] thisdata)

{

if (thisdata.Length>0)

command=thisdata[0];

if (thisdata.Length>5)

{

dataLength =

(((int)thisdata[4]) << 24) +

(((int)thisdata[3]) << 16) +

(((int)thisdata[2]) << 8) +

((int)thisdata[1]);

if (dataLength>0)

{

Data=new Byte[dataLength];

Array.Copy(thisdata,5,Data,0,dataLength);

}

}

}

public Packet(PacketType cmd, byte[] thisdata)

{

Command=cmd;

Data=thisdata;

dataLength=Data.Length;

}

public Packet(PacketType cmd, string thisdata)

{

Command=cmd;

Data=System.Text.Encoding.ASCII.GetBytes(thisdata.ToCharArray());

dataLength=Data.Length;

Debug.WriteLine("setting dataLength to "+dataLength);

}

public byte[] Serialize()

{

int len=this.Length;

byte[] tempcmd=new byte[len];

tempcmd[0] = command;

tempcmd[1] = (byte)(dataLength & 0xff);

tempcmd[2] = (byte)((dataLength >> 8) & 0xff);

tempcmd[3] = (byte)((dataLength >>16) & 0xff);

tempcmd[4] = (byte)((dataLength >>24) & 0xff);


if (len>5)

{

Array.Copy(Data,0,tempcmd,5,dataLength);

}

return tempcmd;

}

}

}


Gianmaria I. said:
Hi,
having a BitArray, how can i extract bits to create a System.byte

as in the example...

With
BitArray bits
and
Byte myNewByte

bits[0] to bits[7] => myNewByte
 
Or, you could use the CopyTo method on the BitArray class. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
It has been since beta since I did any of this, but here is some old
source code that might help you out. Someone on this same forum had
helped me about 4 years ago with a similar problem when I was working out
a protocal for an online game. If you look at what I am doing there are a
couple choices that might help you accomplish what you need. Pay special
attention to how I calculate the data length from the first 4 bytes of my
packet, this will probably be your most efficient method of doing what you
want. Here is my code.

using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

using System.Diagnostics;

namespace MudClient

{

/// <summary>

/// Summary description for Packet.

/// </summary>

///

public enum PacketType:byte

{

IgnoreThisPacket,

BroadcastToServer,

LoginToServer,

LoginAccepted,

LoginDenied,

LoginReqestfromServer,

QuitToServer,

QuitToClient,

ServerShuttingDown,

SetCommandLineToClient,

CommandToServer,

CommandToClient,

TextToClient,

PingClient,

PongServer,

AbbrTextToClient,

RequestAbbrTextValue,

SendAbbrTextValue,

ClearAbbrTextOnClient

}

public class Packet

{

private byte command;

private int dataLength=0;

private byte[] Data;

public byte[] PacketData

{

get

{

return Data;

}

set

{

Data=value;

dataLength=Data.Length;

}

}

public int Length

{

get

{

if (Data==null)

return 5;

else

return dataLength+5;

}

}

public PacketType Command

{

get

{

return (PacketType)command;

}

set

{

command=(byte)value;

}

}

public string StrPacketData

{

get {return System.Text.Encoding.ASCII.GetString(Data);}

set

{

Data=System.Text.Encoding.ASCII.GetBytes(value.ToCharArray());

dataLength=Data.Length;

}

}

public Packet()

{

Command=PacketType.IgnoreThisPacket;

}

public Packet(PacketType cmd)

{

Command=cmd;

}

public Packet(byte[] thisdata)

{

if (thisdata.Length>0)

command=thisdata[0];

if (thisdata.Length>5)

{

dataLength =

(((int)thisdata[4]) << 24) +

(((int)thisdata[3]) << 16) +

(((int)thisdata[2]) << 8) +

((int)thisdata[1]);

if (dataLength>0)

{

Data=new Byte[dataLength];

Array.Copy(thisdata,5,Data,0,dataLength);

}

}

}

public Packet(PacketType cmd, byte[] thisdata)

{

Command=cmd;

Data=thisdata;

dataLength=Data.Length;

}

public Packet(PacketType cmd, string thisdata)

{

Command=cmd;

Data=System.Text.Encoding.ASCII.GetBytes(thisdata.ToCharArray());

dataLength=Data.Length;

Debug.WriteLine("setting dataLength to "+dataLength);

}

public byte[] Serialize()

{

int len=this.Length;

byte[] tempcmd=new byte[len];

tempcmd[0] = command;

tempcmd[1] = (byte)(dataLength & 0xff);

tempcmd[2] = (byte)((dataLength >> 8) & 0xff);

tempcmd[3] = (byte)((dataLength >>16) & 0xff);

tempcmd[4] = (byte)((dataLength >>24) & 0xff);


if (len>5)

{

Array.Copy(Data,0,tempcmd,5,dataLength);

}

return tempcmd;

}

}

}


Gianmaria I. said:
Hi,
having a BitArray, how can i extract bits to create a System.byte

as in the example...

With
BitArray bits
and
Byte myNewByte

bits[0] to bits[7] => myNewByte
 
Thanks, I learned something from this too.

Nicholas Paldino said:
Or, you could use the CopyTo method on the BitArray class. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
It has been since beta since I did any of this, but here is some old
source code that might help you out. Someone on this same forum had
helped me about 4 years ago with a similar problem when I was working out
a protocal for an online game. If you look at what I am doing there are
a couple choices that might help you accomplish what you need. Pay
special attention to how I calculate the data length from the first 4
bytes of my packet, this will probably be your most efficient method of
doing what you want. Here is my code.

using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

using System.Diagnostics;

namespace MudClient

{

/// <summary>

/// Summary description for Packet.

/// </summary>

///

public enum PacketType:byte

{

IgnoreThisPacket,

BroadcastToServer,

LoginToServer,

LoginAccepted,

LoginDenied,

LoginReqestfromServer,

QuitToServer,

QuitToClient,

ServerShuttingDown,

SetCommandLineToClient,

CommandToServer,

CommandToClient,

TextToClient,

PingClient,

PongServer,

AbbrTextToClient,

RequestAbbrTextValue,

SendAbbrTextValue,

ClearAbbrTextOnClient

}

public class Packet

{

private byte command;

private int dataLength=0;

private byte[] Data;

public byte[] PacketData

{

get

{

return Data;

}

set

{

Data=value;

dataLength=Data.Length;

}

}

public int Length

{

get

{

if (Data==null)

return 5;

else

return dataLength+5;

}

}

public PacketType Command

{

get

{

return (PacketType)command;

}

set

{

command=(byte)value;

}

}

public string StrPacketData

{

get {return System.Text.Encoding.ASCII.GetString(Data);}

set

{

Data=System.Text.Encoding.ASCII.GetBytes(value.ToCharArray());

dataLength=Data.Length;

}

}

public Packet()

{

Command=PacketType.IgnoreThisPacket;

}

public Packet(PacketType cmd)

{

Command=cmd;

}

public Packet(byte[] thisdata)

{

if (thisdata.Length>0)

command=thisdata[0];

if (thisdata.Length>5)

{

dataLength =

(((int)thisdata[4]) << 24) +

(((int)thisdata[3]) << 16) +

(((int)thisdata[2]) << 8) +

((int)thisdata[1]);

if (dataLength>0)

{

Data=new Byte[dataLength];

Array.Copy(thisdata,5,Data,0,dataLength);

}

}

}

public Packet(PacketType cmd, byte[] thisdata)

{

Command=cmd;

Data=thisdata;

dataLength=Data.Length;

}

public Packet(PacketType cmd, string thisdata)

{

Command=cmd;

Data=System.Text.Encoding.ASCII.GetBytes(thisdata.ToCharArray());

dataLength=Data.Length;

Debug.WriteLine("setting dataLength to "+dataLength);

}

public byte[] Serialize()

{

int len=this.Length;

byte[] tempcmd=new byte[len];

tempcmd[0] = command;

tempcmd[1] = (byte)(dataLength & 0xff);

tempcmd[2] = (byte)((dataLength >> 8) & 0xff);

tempcmd[3] = (byte)((dataLength >>16) & 0xff);

tempcmd[4] = (byte)((dataLength >>24) & 0xff);


if (len>5)

{

Array.Copy(Data,0,tempcmd,5,dataLength);

}

return tempcmd;

}

}

}


Gianmaria I. said:
Hi,
having a BitArray, how can i extract bits to create a System.byte

as in the example...

With
BitArray bits
and
Byte myNewByte

bits[0] to bits[7] => myNewByte
 
Thank you too, u really kind.
Nathan Kovac said:
Thanks, I learned something from this too.

Nicholas Paldino said:
Or, you could use the CopyTo method on the BitArray class. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
It has been since beta since I did any of this, but here is some old
source code that might help you out. Someone on this same forum had
helped me about 4 years ago with a similar problem when I was working
out a protocal for an online game. If you look at what I am doing there
are a couple choices that might help you accomplish what you need. Pay
special attention to how I calculate the data length from the first 4
bytes of my packet, this will probably be your most efficient method of
doing what you want. Here is my code.

using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

using System.Diagnostics;

namespace MudClient

{

/// <summary>

/// Summary description for Packet.

/// </summary>

///

public enum PacketType:byte

{

IgnoreThisPacket,

BroadcastToServer,

LoginToServer,

LoginAccepted,

LoginDenied,

LoginReqestfromServer,

QuitToServer,

QuitToClient,

ServerShuttingDown,

SetCommandLineToClient,

CommandToServer,

CommandToClient,

TextToClient,

PingClient,

PongServer,

AbbrTextToClient,

RequestAbbrTextValue,

SendAbbrTextValue,

ClearAbbrTextOnClient

}

public class Packet

{

private byte command;

private int dataLength=0;

private byte[] Data;

public byte[] PacketData

{

get

{

return Data;

}

set

{

Data=value;

dataLength=Data.Length;

}

}

public int Length

{

get

{

if (Data==null)

return 5;

else

return dataLength+5;

}

}

public PacketType Command

{

get

{

return (PacketType)command;

}

set

{

command=(byte)value;

}

}

public string StrPacketData

{

get {return System.Text.Encoding.ASCII.GetString(Data);}

set

{

Data=System.Text.Encoding.ASCII.GetBytes(value.ToCharArray());

dataLength=Data.Length;

}

}

public Packet()

{

Command=PacketType.IgnoreThisPacket;

}

public Packet(PacketType cmd)

{

Command=cmd;

}

public Packet(byte[] thisdata)

{

if (thisdata.Length>0)

command=thisdata[0];

if (thisdata.Length>5)

{

dataLength =

(((int)thisdata[4]) << 24) +

(((int)thisdata[3]) << 16) +

(((int)thisdata[2]) << 8) +

((int)thisdata[1]);

if (dataLength>0)

{

Data=new Byte[dataLength];

Array.Copy(thisdata,5,Data,0,dataLength);

}

}

}

public Packet(PacketType cmd, byte[] thisdata)

{

Command=cmd;

Data=thisdata;

dataLength=Data.Length;

}

public Packet(PacketType cmd, string thisdata)

{

Command=cmd;

Data=System.Text.Encoding.ASCII.GetBytes(thisdata.ToCharArray());

dataLength=Data.Length;

Debug.WriteLine("setting dataLength to "+dataLength);

}

public byte[] Serialize()

{

int len=this.Length;

byte[] tempcmd=new byte[len];

tempcmd[0] = command;

tempcmd[1] = (byte)(dataLength & 0xff);

tempcmd[2] = (byte)((dataLength >> 8) & 0xff);

tempcmd[3] = (byte)((dataLength >>16) & 0xff);

tempcmd[4] = (byte)((dataLength >>24) & 0xff);


if (len>5)

{

Array.Copy(Data,0,tempcmd,5,dataLength);

}

return tempcmd;

}

}

}


Hi,
having a BitArray, how can i extract bits to create a System.byte

as in the example...

With
BitArray bits
and
Byte myNewByte

bits[0] to bits[7] => myNewByte
 

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

Back
Top