473,484 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[Q]Packing messages into a byte buffer

Hi Group,

I am trying without much luck to pack messages into a byte buffer
using get and set accessors.

I have attached a simplified example of what I am trying to do using
byte variables.
(My real data is not byte variables.)

I wish to be able to populate a field in the message header and then
include this in a
init message, and finally once all the message is transfered to the
byte buffer populate
the message header length.

However I alway get zero for my message header length when I write to
the console.

I am confused I think with the get and set accessor.

Could anyone suggest where I have gone wrong.

Mark

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
msgHdr msgHeader = new msgHdr();
// Don't set message length here.
msgHeader.messageId = 100;

initMessage theInitrequest = new initMessage();
theInitrequest.messageHeader = msgHeader;
theInitrequest.version = 200;
//
// Hopefully here
// Initrequest.messageHeader.messageLength = 3
// Initrequest.messageHeader.messageId = 100
// Initrequest.version = 200;

Console.WriteLine(theInitrequest.messageHeader.mes sageLength.ToString());

Console.WriteLine(theInitrequest.messageHeader.mes sageId.ToString());
Console.WriteLine(theInitrequest.version.ToString( ));
// and
// Initrequest.Data [0] = 3
// Initrequest.Data [1] = 100;
// Initrequest.Data [2] = 200;
Console.WriteLine(theInitrequest.Data[0].ToString());
Console.WriteLine(theInitrequest.Data[1].ToString());
Console.WriteLine(theInitrequest.Data[2].ToString());

}
}
public class Utility
{
public static void Pack(byte src, ref byte[] Data, ref uint
Index)
{
Data[Index++] = src;
}

public static void Unpack(ref byte dst, byte[] Data, ref uint
Index)
{
dst = Data[Index++];
}
}

public class initMessage
{
public msgHdr messageHeader;
public byte version;

private byte[] _internalBuffer;

public byte[] Data
{
set
{
uint index = 0;
Array.Copy(_internalBuffer, index, messageHeader.Data,
0, messageHeader.Data.Length);
index = index +
System.Convert.ToByte(messageHeader.Data.Length);
Utility.Unpack(ref version, value, ref index);
}
get
{
uint index = 0;
_internalBuffer = new byte[20];
Array.Copy(messageHeader.Data, 0, _internalBuffer,
index, messageHeader.Data.Length);
index = index +
System.Convert.ToByte(messageHeader.Data.Length);
Utility.Pack(version, ref _internalBuffer, ref index);
messageHeader.messageLength =
System.Convert.ToByte(index);
return _internalBuffer;
}
}
}

public class msgHdr
{
public byte messageLength;
public byte messageId;

private byte[] _internalBuffer = new byte[2];
public byte[] Data
{
set
{
uint index = 0;
Utility.Unpack(ref messageLength, value, ref index);
Utility.Unpack(ref messageId, value, ref index);
}
get
{
uint index = 0;
Utility.Pack(messageLength, ref _internalBuffer, ref
index);
Utility.Pack(messageId, ref _internalBuffer, ref
index);
return _internalBuffer;
}
}
}
}

May 5 '07 #1
0 1445

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

Similar topics

1
11421
by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
1
7801
by: Steve | last post by:
Please take a look at the simple code segment below and advise me what is wrong. According to the help and examples I've seen it should work unless I misunderstand something. The problem is...
1
3164
by: Gajendra | last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing for example while using the structur struc double...
6
2726
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)...
10
98694
by: D. Yates | last post by:
All, Is there a faster way to do this in C#: byte buffer; buffer = new byte; for(int i = 0; i < buffer.Length; i++) buffer = 0;
5
2763
by: Cichy | last post by:
Hello, I'm writing a Client-Server application using sockets (asynchronous). There is a Server (Master) which accepts incoming connections, and Client (Slave). Afetr establishing connections...
0
1860
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will...
2
1205
by: cmrhema | last post by:
Hi, We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice. The whole code is produced below....
10
6340
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
7080
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
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
7140
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...
1
6811
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
7209
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
5403
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4527
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...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
588
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.