473,378 Members | 1,138 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,378 software developers and data experts.

reading "binary struct"

Hi!
I want to read with C# some binary file with well defined binary structure.
In C++ I was declaring appropriate struct, like:

typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1;
REKORD1 buff ;

and then read record from file with
read (file, (void *) &buff, sizeof (REKORD1));

Is this technique possible with C#? Now I have to "manually" bond together
particular values with ReadByte().

Thanks for any help
Paul
Nov 16 '05 #1
3 4274
Hi Paul,

It is possible with C# to load a byte array into a struct with something like

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct REKORD1
{
public byte b1;
public ushort w1;
public ushort w2;
public uint dw1;
}

....

byte[] data = GetData(); // read from file or something
int size = Marshal.SizeOf(typeof(REKORD1);

IntPtr ip = Marshal.AllocHGlobal(size);
GCHandle gch = GCHandle.Alloc(ip, GCHandleType.Pinned);

Marshal.Copy(data, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marshal.PtrToStructure(ip, t);

gch.Free();
Marshal.FreeHGlobal(ip);

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi,
inline

"Paweł" <no*****@nowhere.com> wrote in message
news:41********@news.home.net.pl...
Hi!
I want to read with C# some binary file with well defined binary
structure.
In C++ I was declaring appropriate struct, like:

typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1;
REKORD1 buff ;

and then read record from file with
read (file, (void *) &buff, sizeof (REKORD1));

Is this technique possible with C#? Now I have to "manually" bond together
particular values with ReadByte().
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct REKORD1
{
byte b1;
ushort w1;
ushort w2;
uint dw1;
}

byte[] data; // read from file
GCHandle gData = GCHandle.Alloc(data, GCHandleType.Pinned);
REKORD1 newRecord =
(REKORD1)Marshal.PtrToStructure(gData.AddrOfPinnen dObject(),
typeof(REKORD1));
gch.Free();
HTH,
greetings


Thanks for any help
Paul

Nov 16 '05 #3
Hi

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsd2kk9h3klbvpo@stone...
Hi Paul,

It is possible with C# to load a byte array into a struct with something
like

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct REKORD1
{
public byte b1;
public ushort w1;
public ushort w2;
public uint dw1;
}

...

byte[] data = GetData(); // read from file or something
int size = Marshal.SizeOf(typeof(REKORD1);

IntPtr ip = Marshal.AllocHGlobal(size);
//GCHandle gch = GCHandle.Alloc(ip, GCHandleType.Pinned);

Marshal.Copy(data, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marshal.PtrToStructure(ip, t);

//gch.Free();
Marshal.FreeHGlobal(ip);
You allocate memory on the heap and then you pin it ? Memory obtained from
the unmanaged heap (AllocHGlobal) does not need pinning.
If you pin the byte array you can get a pointer to it and then use that for
PtrToStructure().
greetings


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

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

Similar topics

6
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
3
by: poifull | last post by:
Hi All, What is the proper way to read a binary file into a byte? I am using BinaryReader to read from a Stream and call the ReadByte method of the BinaryReader object. The method I'm using...
10
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
20
by: Francine.Neary | last post by:
People seem to have different views as to where the C reserved word "struct" comes from. One explanation is that it is a shortening of "structure", and another is that it is an acronym for "single...
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
4
by: MetaKith | last post by:
Hi, Please have a look on the next code : struct foo{ float b; char z; }; struct foo foo_init = {234.77,"xcdy"}; struct foo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.