473,769 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert from byte[] to structure value

Hey,

My buffer contains a short int, some char[], and a structure in form of
a byte array.
Read the string as:
TextBox4.Text = System.Text.Enc oding.ASCII.Get String(buffer1, 0, 31);

Read the int as:
TextBox2.Text = BitConverter.To Int16(buffer,0) .ToString();

How do I get the structure value to display.

Thanks alot..

Aug 9 '06 #1
5 3470
"moni" <mo*******@gmai l.comwrote in news:1155145116 .321730.52460
@b28g2000cwb.go oglegroups.com:
How do I get the structure value to display.

You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also look
into serializing / deserializing the structure.

-mdb
Aug 9 '06 #2

Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

I am copying 4 bytes which are part of my structure into buffer1 which
is a byte array.
Here pBuffer is a ptr to the Memory Mapped File I have.

In order to read from this array I am doing this:

TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmai l.comwrote in news:1155145116 .321730.52460
@b28g2000cwb.go oglegroups.com:
How do I get the structure value to display.

You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also look
into serializing / deserializing the structure.

-mdb
Aug 9 '06 #3
Could there be an endian problem? Is the data coming from another machine
or was the data put in network byte order before being sent?

jim

"moni" <mo*******@gmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>
Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

I am copying 4 bytes which are part of my structure into buffer1 which
is a byte array.
Here pBuffer is a ptr to the Memory Mapped File I have.

In order to read from this array I am doing this:

TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
>"moni" <mo*******@gmai l.comwrote in news:1155145116 .321730.52460
@b28g2000cwb.g ooglegroups.com :
How do I get the structure value to display.


You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also
look
into serializing / deserializing the structure.

-mdb

Aug 10 '06 #4
The data here is being written into a memory mapped file by a service
that is running on the machine. My GUI program is reading the data from
the Memory mapped file.

I dont think it can be an endian problem because I have managed to read
all other string values, short int values correctly .

Am I reading the structure value correctly when I do,

TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

where,

Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

is copying 4 btes of data onto buffer1 from the ptr pointed to the MMF.

Thanks.

Jim H wrote:
Could there be an endian problem? Is the data coming from another machine
or was the data put in network byte order before being sent?

jim

"moni" <mo*******@gmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .

Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

I am copying 4 bytes which are part of my structure into buffer1 which
is a byte array.
Here pBuffer is a ptr to the Memory Mapped File I have.

In order to read from this array I am doing this:

TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmai l.comwrote in news:1155145116 .321730.52460
@b28g2000cwb.go oglegroups.com:

How do I get the structure value to display.



You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also
look
into serializing / deserializing the structure.

-mdb
Aug 10 '06 #5
In C/C++, you're guaranteed that fields are stored in memory in the
order they're declared. For example, (assuming x86) in

class foo {
int x;
double y;
int z;
};

the memory layout will be x, then y, then z. But as you may be aware,
there may be alignment issues. Ignoring vtable issues and the like, x is
at offset 0, but y probably isn't at offset 4, but at offset 8 (and z at
offset 16). Some CPU architectures perform better if data is aligned on
"natural boundaries" (and some will actually abort if the data isn't
aligned). So doubles are preferentially stored at addresses that are a
multiple of 8, and there would be 4 bytes of padding (uninitialized,
ignored data) between x and y. You must (again, talking just C/C++) use
some extra-language feature (usually #pragma) to adjust (e.g. eliminate)
these pad bytes).

C# addresses this issue by ***not*** guaranteeing that data is stored in
the order you declare. In the above case, it might store data in memory
in the order y, x, z. Or even y, z, x. Or x, z, y (or z, x, y). All of
which satisfy alignment rules, and save 4 bytes of padding per instance.

To get around this, prefix your class with
[StructLayout(La youtKind.Sequen tial)]
class foo ...

You'll also need a
using System.Runtime. InteropServices ;
for the StructLayoutAtt ribute et al.

HTH

P.S. Side note: Without StructLayout, the alignments are *not*
determined at compile time, since the compiler doesn't know if this will
be run on a 32-bit architecture, 64-bit, different CPU families (x86,
PowerPC, etc), all of which may have different alignment rules.

moni wrote:
The data here is being written into a memory mapped file by a service
that is running on the machine. My GUI program is reading the data from
the Memory mapped file.

I dont think it can be an endian problem because I have managed to read
all other string values, short int values correctly .

Am I reading the structure value correctly when I do,

TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

where,

Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

is copying 4 btes of data onto buffer1 from the ptr pointed to the MMF.

Thanks.

Jim H wrote:
>Could there be an endian problem? Is the data coming from another machine
or was the data put in network byte order before being sent?

jim

"moni" <mo*******@gmai l.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
>>>

Marshal.Cop y( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

I am copying 4 bytes which are part of my structure into buffer1 which
is a byte array.
Here pBuffer is a ptr to the Memory Mapped File I have.

In order to read from this array I am doing this:

TextBox6.Te xt = BitConverter.To UInt32(buffer1, 0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmai l.comwrote in news:1155145116 .321730.52460
@b28g2000cwb .googlegroups.c om:

How do I get the structure value to display.
>
>
You have to read each value of the structure in order it is stored in the
array. If you are the one writing the byte array, then you could also
look
into serializing / deserializing the structure.

-mdb
Aug 10 '06 #6

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

Similar topics

19
4152
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
7
52486
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte array.. Thanks & Regards Prakash Prabhu
2
12198
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
8
16395
by: Ken Dopierala Jr. | last post by:
Hi, I'm reading the header file of a PCX image and I need to convert 2 bytes to a short. How would I go about doing this? I know that they are bytes 8 & 9 in my byte array. I'm not sure how to take those two and convert them into a short though. In C I would just use a union and assign them accordingly. Thanks! Ken.
9
12688
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
25
7304
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As Byte Dim b As Byte
6
4258
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Type INFO name(1 To 3) As String * 41 address As String * 41 End Typ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
4
4563
by: Joao Tomas | last post by:
Please, can someone help me to translate this single function to VB.NET ? ' DECLARATIONS Private Const BMP_MAGIC_COOKIE As Short = 19778 Private Structure BITMAPFILEHEADER '14 bytes Dim bfType As Short
6
4465
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005, .net 2 and C# for windows application. I need to convert a IntPtr to a byte to be able to add a meetingBlob data to the meeting class object in Active Directory schema. I get a "unspecified error" if I tried to add the data before converting to a byte in "deNewContextObject.Properties.Add((object)blob.pData);". public struct Blob { public IntPtr pData; public int nLength;
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5307
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.