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

Cast a byte array to a structure

Hello,

I need to interact with the Win32 API "BackupRead," and it returns a byte
array as one of its parameters.

I need to be able to cast the first part of the byte array into a special
structure that describes the data contained in the rest of the byte array.

How can I do this? For example:

byte [] A = new byte[100];

struct SpecialStruct
{
int a;
string b;
};

// I want to be able to perform a cast-like operation such as this:

SpecialStruct S = (SpecialStruct) A; // Remember, A is the byte array

How can I accomplish something like this in C#??

Thank you very much,
Rich
Nov 17 '05 #1
1 2783
> I need to interact with the Win32 API "BackupRead," and it returns a byte
array as one of its parameters.
Only if you declare it that way. You can also make the second parameter an
IntPtr and pass in a pointer to a native memory buffer. Then you can use
Marshal.PtrToStructure to retrieve the header struct.

I need to be able to cast the first part of the byte array into a special
structure that describes the data contained in the rest of the byte array.

How can I do this? For example:

byte [] A = new byte[100];

struct SpecialStruct
{
int a;
string b;
};

// I want to be able to perform a cast-like operation such as this:

SpecialStruct S = (SpecialStruct) A; // Remember, A is the byte array

How can I accomplish something like this in C#??


Well for trivial structures like these you can do something like

SpecialStruct S;
S.a = BitConverter.ToInt32(A, 0);
S.b = Encoding.Unicode.GetString(A, 4, length); // replace Unicode with
appropriate encoding
Mattias

Nov 17 '05 #2

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

Similar topics

12
by: Martin | last post by:
Two questions relating to FAQ answer 12.42. (1) In the statement s.i16 |= (unsigned)(getc(fp) << 8); i16 is declared int. The reason for casting to (unsigned) is explained as guarding...
3
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...
5
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...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
5
by: Eric bouxirot | last post by:
hi, i'd like to do a "cast", who seem to be very complicated in .NET. in C standard it's so easy... i explain : i have made an app based on plugin architecture in VB.NET. all work fine.. ...
9
by: Leon_Amirreza | last post by:
How Can I cast a uint type to a byte? Does the following code do this? uint a = 5; byte b = new byte; b = (byte)a;
10
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...
2
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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.