473,396 Members | 2,068 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,396 software developers and data experts.

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.Encoding.ASCII.GetString(buffer1, 0, 31);

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

How do I get the structure value to display.

Thanks alot..

Aug 9 '06 #1
5 3442
"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.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.ToUInt32(buffer1,0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.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*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.ToUInt32(buffer1,0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
>"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.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.ToUInt32(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*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.ToUInt32(buffer1,0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.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(LayoutKind.Sequential)]
class foo ...

You'll also need a
using System.Runtime.InteropServices;
for the StructLayoutAttribute 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.ToUInt32(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*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.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.ToUInt32(buffer1,0).ToString();

But it is giving me garbage values.

Any clue?

Thanks
Michael Bray wrote:
"moni" <mo*******@gmail.comwrote in news:1155145116.321730.52460
@b28g2000cwb.googlegroups.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 #6

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

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
7
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...
2
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
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...
9
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
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...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
4
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.