473,396 Members | 2,147 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.

Converting byte[] to fixed byte[]

Given the following bit of code, the PtrToStructure is not correctly
copying the data into the PDU class. After it executes, the
pdu.buffer[] array looks nothing like the data[] array that I tried to
copy from. Help?

[StructLayout(LayoutKind.Explicit)]
public unsafe struct PDU {
public const int MAX_PDU_SIZE = 1400;
[FieldOffset(0)]
public fixed byte buffer[MAX_PDU_SIZE];
[FieldOffset(0)]
public Header header;
}

class Manager {
unsafe public void processData(byte[] data) {
//
// Convert data to a PDU
//
PDU pdu;
IntPtr arrayPtr = Marshal.UnsafeAddrOfPinnedArrayElement(
data, PDU.MAX_PDU_SIZE);
pdu = (PDU)Marshal.PtrToStructure(arrayPtr, typeof(PDU));

// More processing ...
}
}

static void Main(string[] args) {
// Testing...
Manager manager = new Manager();
byte[] data = new byte[1400];
for (int i = 0; i < 1400; i++) {
data[i] = (byte)((i + 1)%256);
}
manager.processData(data);
}
Oct 27 '06 #1
2 3831
IntPtr arrayPtr = Marshal.UnsafeAddrOfPinnedArrayElement(
data, PDU.MAX_PDU_SIZE);
Here you get the address of the _last_ element in the array. So what
you're processing is whatever is in memory after the array. The second
parameter should probably be 0 in your case.

Also, you never pin the data array. Therefore the address may become
invalid at any time.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 27 '06 #2
Mattias Sjögren wrote:
> IntPtr arrayPtr = Marshal.UnsafeAddrOfPinnedArrayElement(
data, PDU.MAX_PDU_SIZE);

Here you get the address of the _last_ element in the array. So what
you're processing is whatever is in memory after the array. The second
parameter should probably be 0 in your case.

Also, you never pin the data array. Therefore the address may become
invalid at any time.
Mattias
Thank you.
Oct 27 '06 #3

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

Similar topics

2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
5
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
3
by: Howler | last post by:
Hello all, I am having a hard time seeing what I am doing wrong with a program I am having to write that converts pbm monochrome images into a similar pgm file. The problem I am having is...
0
by: Alexf2 | last post by:
Hello! I'm reading binary file exported from mainframe's tape. There used COBOL data types. Some date fields are represented by PIC '9999', but some - by FIXED(7). The first one I have recognized...
2
by: Bjorn Sagbakken | last post by:
Hi. This story is about uploading jpg's, then resize them to fixed width or height and storing them to an SQL table. The only way I have found so far is to read the uploaded file to an...
0
by: hillman | last post by:
Hello, Maestros. Following on from the posts on creating structs from a byte. The methods described there work well. But I seem to have struck a snag that some might find interesting. The C/C++...
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...
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: 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
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
Oralloy
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,...
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
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...
0
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,...

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.