I need to convert a byte array to a structure with conversion on the byte
order. I found the below deserilizer which works for getting the data
converted to a structure but it's in the wrong byte order. Basically it's
the whole big/little endian problem.
Is there someway to do this directly or do I need to use the bitconverter
step the array myself?
Regards,
John
public static object RawDeserializeE x( byte[] rawdatas, Type anytype )
{
int rawsize = Marshal.SizeOf( anytype );
if( rawsize > rawdatas.Length )
return null;
GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pi nned );
IntPtr buffer = handle.AddrOfPi nnedObject();
object retobj = Marshal.PtrToSt ructure( buffer, anytype );
handle.Free();
return retobj;
} 5 2363
Hi John,
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to
convert the byte array to an object. If there is any misunderstandin g,
please feel free to let me know.
Thanks for sharing your code here. I think the code is correct. Whether the
byte array can be deserialized correctly depends on how it was serialized.
When marshalling an object to a byte array, I think we can try to use
serialization in .NET. We can use a BinaryFormatter to do serialization and
deserialization . Here is an example: http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemrunt imeserializatio nformattersbina rybinaryformatt erclassserializ e
topic.asp
HTH. If anything is unclear, please feel free to reply to the post.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Kevin,
Sort of no... in the concept of all things are objects yes. In the concept
of the object has been serialized and is self defining no.
I have a C++ program on non-Intel process that is sending my application
data via a serial port. Basically a memory dump. The memory contains
structures. I need to convert the serial data back to a structure for use
by C#.NET.
Example structure (simplified):
[StructLayout(La youtKind.Sequen tial, Pack=4)]
struct tst
{
UInt16 f1;
UInt32 f2;
}
Example Data stream:
{0x00,0x20,0x00 ,0x00,0x00,0x50 }
The code from my first message deserialzes it but the endian order is
incorrect.
Should end up with:
tst.f1 = 0x0020
tst.f2 = 0x00000050
This would work but the endian order is incorrect:
tst.f1 = System.BitConve rter.ToUInt16(d ata, 0);
tst.f2 = System.BitConve rter.ToUInt32(d ata, 2);
So I end up do this:
tst.f1 = data[0]<<8 + data[1];
tst.f2 = data[2]<<24 + data[3]<<16 + data[4]<<8 + data[5]
Which can be tedious on long structures and a maintenance problem.
So is there a better way?
Regards,
John
Hi John,
I think what you suppose is correct. It is the big/little endian problem.
Intel CPUs swaps the high 8bit and low 8bit when storing data in memory. As
far as I know, there is no direct convertion in .NET. I think you have to
write your own code to swap them back.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Kevin,
Thanks for the response... Just wanted to make sure before I reinvented the
wheel yet again.
Regards,
John
You're welcome, John.
Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: x-pander |
last post by:
given the code:
<file: c.c>
typedef int quad_t;
void w0(int *r, const quad_t *p)
{
*r = (*p);
}
|
by: Pablo Gutierrez |
last post by:
I have a C# method that reads Binary data (BLOB type) from a database
and returns the data an array of bytes
(i.e byte outbyte = new byte;).
The BLOB column is saved into the database by a C program (UNIX) as an
array of "struct point" where
struct point //C structure
{
int Time; //32 bits
|
by: Joe Thompson |
last post by:
Hi
I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do I do this in C# - that is, I want to declare a byte array, assign a structure to it, and then be able to populate the structure's fields, then use the byte array
Thank you
Joe
|
by: ORC |
last post by:
Hi,
I have posted almost the same message in the compact framework group, but it
seems to got lost in a deep message structure :-) so I'll try again:
I'll receive a byte array from a serial port. The byte arrays are actually
serialized structures with alignments of 4 (pack = 4). The structures were
originally written in C++ and looks e.g. like this:
struct Parameters
{
double CreatedDate;
|
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
| |
by: Ronodev.Sen |
last post by:
i have a C# program that is sending data in a byte array through a
socket.
the VC++ application server receives data in teh following format....
typedef struct advice
{
header sHdr;
char Code;
char pn;
|
by: O.B. |
last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.
/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
|
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 and then
convert that to a byte array, pass it to the device, then get a reply and
then convert that to a structure.
I'm having issues with making sure what I've coded is correct. Cant figure
out how to define an array in structure that is a...
|
by: O.B. |
last post by:
When using Marshal to copy data from a byte array to the structure
below, only the first byte of the "other" array is getting copied from
the original byte array. What do I need to specify to get
Marshal.PtrToStructure to copy the all the data into the "other"
array?
unsafe public struct DeadReckoning
{
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |