Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
AnswerRe: Convert struct to byte[] Pin
Le centriste8-Mar-06 1:06
Le centriste8-Mar-06 1:06 
GeneralRe: Convert struct to byte[] Pin
Divyang Mithaiwala8-Mar-06 1:35
Divyang Mithaiwala8-Mar-06 1:35 
GeneralRe: Convert struct to byte[] Pin
Le centriste8-Mar-06 3:25
Le centriste8-Mar-06 3:25 
GeneralRe: Convert struct to byte[] Pin
Divyang Mithaiwala8-Mar-06 3:34
Divyang Mithaiwala8-Mar-06 3:34 
AnswerRe: Convert struct to byte[] Pin
Andy Moore8-Mar-06 4:06
Andy Moore8-Mar-06 4:06 
GeneralRe: Convert struct to byte[] Pin
Andy Moore8-Mar-06 4:11
Andy Moore8-Mar-06 4:11 
GeneralRe: Convert struct to byte[] Pin
Divyang Mithaiwala8-Mar-06 17:25
Divyang Mithaiwala8-Mar-06 17:25 
AnswerRe: Convert struct to byte[] Pin
mcljava12-Mar-06 15:34
mcljava12-Mar-06 15:34 
I deal with packets to byte streams quite frequently in client/server applications. Packets are essentially data structures with an assortment of binary and ascii or whatever formatted data. Here are a couple functions you could use, and polymorhisize as you need:

<br />
		public int Encode( int i32Value, ref byte[] pdu, int off )<br />
		{<br />
			byte[] byte32Int = new byte[ cBytesPerWord32 ];<br />
			int netValue = System.Net.IPAddress.HostToNetworkOrder( i32Value );<br />
			byte32Int = BitConverter.GetBytes( netValue );<br />
			Array.Copy( byte32Int, 0, pdu, off, cBytesPerWord32 );<br />
			return cBytesPerWord32;<br />
		}<br />
<br />
		public int Decode( ref byte[] pdu, int off, ref uint u32Value )<br />
		{<br />
			uint netValue = BitConverter.ToUInt32( pdu, off );<br />
			u32Value = (uint)System.Net.IPAddress.NetworkToHostOrder( (int) netValue );<br />
			return cBytesPerWord32;<br />
		}<br />


Although the function above only deals with 32-bit uint's you can imagine how easy it is to add support for 16-bit and 64-bit integers/unsigned integers. Strings are variable length in most cases otherwise a simple Array.Copy() would suffice. So if you have to pass strings, at a minimum you'll be best served with a length byte and quite possible some sort of a type or tag byte - especialy if you have lots of string elements. That way you can handle strings in any order.

Good luck

Mike Luster
CTI/IVR/Telephony SME
Questionaccessing remote registry Pin
sendmadhavan8-Mar-06 0:21
sendmadhavan8-Mar-06 0:21 
AnswerRe: accessing remote registry Pin
Le centriste8-Mar-06 1:07
Le centriste8-Mar-06 1:07 
GeneralRe: accessing remote registry Pin
sendmadhavan8-Mar-06 1:58
sendmadhavan8-Mar-06 1:58 
GeneralRe: accessing remote registry Pin
Le centriste8-Mar-06 2:01
Le centriste8-Mar-06 2:01 
GeneralRe: accessing remote registry Pin
sendmadhavan8-Mar-06 2:08
sendmadhavan8-Mar-06 2:08 
GeneralRe: accessing remote registry Pin
Le centriste8-Mar-06 2:10
Le centriste8-Mar-06 2:10 
GeneralRe: accessing remote registry Pin
sendmadhavan8-Mar-06 2:13
sendmadhavan8-Mar-06 2:13 
Questiondisable button functions Pin
Dave McCool7-Mar-06 23:31
Dave McCool7-Mar-06 23:31 
AnswerRe: disable button functions Pin
gnjunge7-Mar-06 23:47
gnjunge7-Mar-06 23:47 
GeneralRe: disable button functions Pin
Dave McCool8-Mar-06 0:39
Dave McCool8-Mar-06 0:39 
QuestionStart Button panel context menu Pin
Dave McCool7-Mar-06 23:29
Dave McCool7-Mar-06 23:29 
Questiontreeview windowsform control in csharp Pin
eng.islam7-Mar-06 23:23
eng.islam7-Mar-06 23:23 
AnswerRe: treeview windowsform control in csharp Pin
Chandana Subasinghe8-Mar-06 0:27
Chandana Subasinghe8-Mar-06 0:27 
Questionplaying movie Pin
Mihai si atat7-Mar-06 23:22
Mihai si atat7-Mar-06 23:22 
Questionwhat c# has equal to my in vb2005? Pin
Amir Jalaly7-Mar-06 22:56
Amir Jalaly7-Mar-06 22:56 
AnswerRe: what c# has equal to my in vb2005? Pin
J4amieC7-Mar-06 23:06
J4amieC7-Mar-06 23:06 
AnswerRe: what c# has equal to my in vb2005? Pin
Steve Hansen8-Mar-06 0:56
Steve Hansen8-Mar-06 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.