PC Review


Reply
Thread Tools Rate Thread

Cannot convert from byte* to byte[]

 
 
valentin tihomirov
Guest
Posts: n/a
 
      6th May 2007
The conversion shold be trivial -- no conversion copy. In essense, both
types are pointers to byte array. The difference is purely semantical.

unsafe {
byte b = 1;
byte[] data = &b;
}


Actually, I considered establishing data reference in a structure:

[StructLayout(LayoutKind.Sequential)]
struct Struct {
public UInt32 count;
public byte[] pData;
}


 
Reply With Quote
 
 
 
 
Alun Harford
Guest
Posts: n/a
 
      6th May 2007
valentin tihomirov wrote:
> The conversion shold be trivial -- no conversion copy. In essense, both
> types are pointers to byte array. The difference is purely semantical.
>
> unsafe {
> byte b = 1;
> byte[] data = &b;
> }


The difference isn't purely semantic in C#. Unlike C, C# arrays have
additional information such as length.
To get a byte[] from a byte*, use

Marshal.Copy(new IntPtr(pointerToConvert), byteArrayName, 0, arraySize);

Alun Harford
 
Reply With Quote
 
valentin tihomirov
Guest
Posts: n/a
 
      6th May 2007
> The difference isn't purely semantic in C#. Unlike C, C# arrays have
> additional information such as length.


There should be a way to define a plain array then. Like in Delphi, which
exposes dynamic arrays with variable length and system managed lifetime
while retaining the basic ones.

I have succeeded declaring the array as IntPtr with explicit casting array
to IntPtr and, later, by declaring the array as byte*. The problem of C#, as
I noob see it, is that you should have different declarations of structures
and P/Invokable functions for safe and unsafe code. IntPtr is opaque but is
allowed in safe context; static arrays is convetient for unsafe but
precludes using structure in safe mode.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot convert 'ref byte[]' to 'ref byte' Pramod Microsoft C# .NET 6 12th Jun 2010 12:45 AM
How to convert byte() to string, and from string back to byte() moondaddy Microsoft VB .NET 4 15th Aug 2005 03:25 PM
Convert from managed byte[] to void* and back to byte[] again jim.wiese@gmail.com Microsoft Dot NET 1 15th Feb 2005 06:11 AM
How to Convert Binary Coded Hex Byte Array to Byte Charles Law Microsoft VB .NET 25 2nd Jun 2004 02:21 PM
Convert native byte array (pointer) to managed byte[] Dave Microsoft Dot NET 1 13th Aug 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:01 AM.