473,418 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Byte building

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

Nov 17 '05 #1
5 1679
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gianmaria I." <no*********@hothot.it> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
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.ToC harArray());

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." <no*********@hothot.it> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #3
Or, you could use the CopyTo method on the BitArray class. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:uN*************@TK2MSFTNGP15.phx.gbl...
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.ToC harArray());

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." <no*********@hothot.it> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #4
Thanks, I learned something from this too.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uj**************@tk2msftngp13.phx.gbl...
Or, you could use the CopyTo method on the BitArray class. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:uN*************@TK2MSFTNGP15.phx.gbl...
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.ToC harArray());

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." <no*********@hothot.it> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #5
Thank you too, u really kind.
"Nathan Kovac" <na**@tctelco.net> ha scritto nel messaggio
news:eE**************@TK2MSFTNGP12.phx.gbl...
Thanks, I learned something from this too.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uj**************@tk2msftngp13.phx.gbl...
Or, you could use the CopyTo method on the BitArray class. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:uN*************@TK2MSFTNGP15.phx.gbl...
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.ToC harArray());

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." <no*********@hothot.it> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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




Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vai2000 | last post by:
Hi All, I have a ClassA and a worker ClassB. in my ClassA I have a method which calls the following routine for(int idx=0;idx<10;idx++) { ClassB ob=new ClassB(); ThreadPool.QueueUserWorkItem(new...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
3
by: Ashit Vora | last post by:
Hi, I had one small query. I have a data in char pointer (actually 4 char pointers) I wanna make one char pointer out of it (without terminating it with NULL. so cant use STRCPY). I used...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.