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

covert between byte[] and int []

So far, I get the idea that if I want to use both the unmanaged and managed
memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea
that maybe I could use "union" to convert byte[] to int[] and so on. Here is
my source code, I wonder if this will work with GC?

[StructLayout(LayoutKind.Explicit)]

struct MyUnion

{

[FieldOffset(0)]

public int[] a;

[FieldOffset(0)]

public byte[] b;

}

After I define this "union". I can use the following code to alloc and
access the data buffer:
MyUnion mu = new MyUnion();
mu.a = new int[640*480];
//if I like access it as byte[]
byte[] bp = mu.b;

will this work?
Nov 15 '05 #1
13 2112
Ray,

You have two options here. The first is to use unsafe code, and then
cast the pointer to the byte array (which is really a pointer to a location
in memory to a byte) to a pointer to an integer, and then work from there.

If you want a purely managed solution, then I would use the static
BlockCopy method on the Buffer class to copy the bytes from the byte array
to an appropriately-sized integer array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert byte[] to int[] and so on. Here is my source code, I wonder if this will work with GC?

[StructLayout(LayoutKind.Explicit)]

struct MyUnion

{

[FieldOffset(0)]

public int[] a;

[FieldOffset(0)]

public byte[] b;

}

After I define this "union". I can use the following code to alloc and
access the data buffer:
MyUnion mu = new MyUnion();
mu.a = new int[640*480];
//if I like access it as byte[]
byte[] bp = mu.b;

will this work?

Nov 15 '05 #2
Thanks.
Then, what's wrong with my approach?
In my case, I can not use copy for sure because I am talking about buffer
size at least 2M bytes.
About the unsafe code, how can I cast a byte* to a byte[]? I need byte[]
because the stream reader (I need read image data from file some times) need
byte[] as it's parameter.

Again, go back my approach, is there something wrong?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:us*************@TK2MSFTNGP11.phx.gbl...
Ray,

You have two options here. The first is to use unsafe code, and then
cast the pointer to the byte array (which is really a pointer to a location in memory to a byte) to a pointer to an integer, and then work from there.

If you want a purely managed solution, then I would use the static
BlockCopy method on the Buffer class to copy the bytes from the byte array
to an appropriately-sized integer array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
So far, I get the idea that if I want to use both the unmanaged and managed
memory, I can not avoid memory copy. But I DO need to avoid it. I get a

idea
that maybe I could use "union" to convert byte[] to int[] and so on.

Here is
my source code, I wonder if this will work with GC?

[StructLayout(LayoutKind.Explicit)]

struct MyUnion

{

[FieldOffset(0)]

public int[] a;

[FieldOffset(0)]

public byte[] b;

}

After I define this "union". I can use the following code to alloc and
access the data buffer:
MyUnion mu = new MyUnion();
mu.a = new int[640*480];
//if I like access it as byte[]
byte[] bp = mu.b;

will this work?


Nov 15 '05 #3
Ray,

Your approach would work fine. The only reason I don't like it is
because it associates a copy of the array in question with the field. From
a design point of view, I would rather have two separate variables which
indicate that one is a different representation of the same thing.

Also, I don't like having to define a new type explicitly for this
purpose, when there are methods to do so.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
Then, what's wrong with my approach?
In my case, I can not use copy for sure because I am talking about buffer
size at least 2M bytes.
About the unsafe code, how can I cast a byte* to a byte[]? I need byte[]
because the stream reader (I need read image data from file some times) need byte[] as it's parameter.

Again, go back my approach, is there something wrong?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:us*************@TK2MSFTNGP11.phx.gbl...
Ray,

You have two options here. The first is to use unsafe code, and then cast the pointer to the byte array (which is really a pointer to a

location
in memory to a byte) to a pointer to an integer, and then work from there.
If you want a purely managed solution, then I would use the static
BlockCopy method on the Buffer class to copy the bytes from the byte array to an appropriately-sized integer array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
So far, I get the idea that if I want to use both the unmanaged and

managed
memory, I can not avoid memory copy. But I DO need to avoid it. I get
a idea
that maybe I could use "union" to convert byte[] to int[] and so on.

Here
is
my source code, I wonder if this will work with GC?

[StructLayout(LayoutKind.Explicit)]

struct MyUnion

{

[FieldOffset(0)]

public int[] a;

[FieldOffset(0)]

public byte[] b;

}

After I define this "union". I can use the following code to alloc and
access the data buffer:
MyUnion mu = new MyUnion();
mu.a = new int[640*480];
//if I like access it as byte[]
byte[] bp = mu.b;

will this work?



Nov 15 '05 #4
I do not like "bad code" although it works. So I want to make some thing
clear. In your second approach, you will use a BlockCopy. After I call this
function, the system will COPY all the data from one place to another place
(so, we will get two copy of the data after it) or just give the reference
to another array (so we have only one copy of data but have two refernce of
it.)? I need make this clear because I can not afford the copy in my
application. I am working on a buffer size more the 2M, the COPY again and
again will terribly reduce performance. And, in your first approach, I did
not get the way to cast a pointer (byte*) back to byte[].

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uP**************@TK2MSFTNGP10.phx.gbl...
Ray,

Your approach would work fine. The only reason I don't like it is
because it associates a copy of the array in question with the field. From a design point of view, I would rather have two separate variables which
indicate that one is a different representation of the same thing.

Also, I don't like having to define a new type explicitly for this
purpose, when there are methods to do so.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
Then, what's wrong with my approach?
In my case, I can not use copy for sure because I am talking about buffer
size at least 2M bytes.
About the unsafe code, how can I cast a byte* to a byte[]? I need byte[]
because the stream reader (I need read image data from file some times) need
byte[] as it's parameter.

Again, go back my approach, is there something wrong?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:us*************@TK2MSFTNGP11.phx.gbl...
Ray,

You have two options here. The first is to use unsafe code, and

then cast the pointer to the byte array (which is really a pointer to a

location
in memory to a byte) to a pointer to an integer, and then work from there.
If you want a purely managed solution, then I would use the static
BlockCopy method on the Buffer class to copy the bytes from the byte array to an appropriately-sized integer array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
> So far, I get the idea that if I want to use both the unmanaged and
managed
> memory, I can not avoid memory copy. But I DO need to avoid it. I get a
idea
> that maybe I could use "union" to convert byte[] to int[] and so on.

Here
is
> my source code, I wonder if this will work with GC?
>
> [StructLayout(LayoutKind.Explicit)]
>
> struct MyUnion
>
> {
>
> [FieldOffset(0)]
>
> public int[] a;
>
> [FieldOffset(0)]
>
> public byte[] b;
>
> }
>
> After I define this "union". I can use the following code to alloc

and > access the data buffer:
> MyUnion mu = new MyUnion();
> mu.a = new int[640*480];
> //if I like access it as byte[]
> byte[] bp = mu.b;
>
> will this work?
>
>



Nov 15 '05 #5
Intriguing, but flawed. If you create an array of length x, then you will
only be able to access the first x elements of the array, regardless of how
you coax the Array reference.

[StructLayout(LayoutKind.Explicit)]
struct MyUnion {
[FieldOffset(0)] public int[] i;
[FieldOffset(0)] public byte[] b;
}

MyUnion mu = new MyUnion();
mu.i = new int[1] { 0x11223344 };

byte[] bp = mu.b;

bp[0] = 0x00 // does what you expect, more or less
bp[1] = 0xaa; // array bounds exception

However this did let us get around the type system -- and opens up a
dangerous hole that perhaps Microsoft should be aware of. This code:

[StructLayout(LayoutKind.Explicit)]
struct MyUnion {
[FieldOffset(0)]
public long[] l;
[FieldOffset(0)]
public byte[] b;
}

MyUnion mu = new MyUnion();
mu.b = new byte[1] { 0xaa };

long[] lp = mu.l;

lp[0] = 0x1122334455667788;

compiles and runs -- and, I assume, writes 7 bytes beyond the end of the
where it should be allowed to write.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uP**************@TK2MSFTNGP10.phx.gbl...
Ray,

Your approach would work fine. The only reason I don't like it is
because it associates a copy of the array in question with the field. From a design point of view, I would rather have two separate variables which
indicate that one is a different representation of the same thing.

Also, I don't like having to define a new type explicitly for this
purpose, when there are methods to do so.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
Then, what's wrong with my approach?
In my case, I can not use copy for sure because I am talking about buffer
size at least 2M bytes.
About the unsafe code, how can I cast a byte* to a byte[]? I need byte[]
because the stream reader (I need read image data from file some times) need
byte[] as it's parameter.

Again, go back my approach, is there something wrong?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:us*************@TK2MSFTNGP11.phx.gbl...
Ray,

You have two options here. The first is to use unsafe code, and

then cast the pointer to the byte array (which is really a pointer to a

location
in memory to a byte) to a pointer to an integer, and then work from there.
If you want a purely managed solution, then I would use the static
BlockCopy method on the Buffer class to copy the bytes from the byte array to an appropriately-sized integer array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Z" <su*********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
> So far, I get the idea that if I want to use both the unmanaged and
managed
> memory, I can not avoid memory copy. But I DO need to avoid it. I get a
idea
> that maybe I could use "union" to convert byte[] to int[] and so on.

Here
is
> my source code, I wonder if this will work with GC?
>
> [StructLayout(LayoutKind.Explicit)]
>
> struct MyUnion
>
> {
>
> [FieldOffset(0)]
>
> public int[] a;
>
> [FieldOffset(0)]
>
> public byte[] b;
>
> }
>
> After I define this "union". I can use the following code to alloc

and > access the data buffer:
> MyUnion mu = new MyUnion();
> mu.a = new int[640*480];
> //if I like access it as byte[]
> byte[] bp = mu.b;
>
> will this work?
>
>



Nov 15 '05 #6
On Mon, 3 Nov 2003 15:17:51 -0500, "Ray Z" <su*********@hotmail.com>
wrote:
So far, I get the idea that if I want to use both the unmanaged and managed
memory, I can not avoid memory copy. But I DO need to avoid it.


<snip>

Well, I got a little bored. Take a look at:

http://home.comcast.net/~aehlers/MemMan.cs

The only copying that occurs is when converting to/from arrays. You
can manipulate the memory with pointers without any copying going on.

Austin Ehlers
Nov 15 '05 #7
> not get the way to cast a pointer (byte*) back to byte[].

You can't cast pointers back to managed types.
Keep it as a byte[] if you need to pass it to a stream. This way you can
also work with it as an int* if you need.

--
William Stacey, MVP

Nov 15 '05 #8

Hi Ray,

Thanks for posting in this group.
I have reviewed your post. Just as Ted points out, your approach of using
union is almost a good workaround, but it has some problem.
You can find the detailed problems in Ted's post.(It is mainly due to .Net
maintains the readonly length property of array).

While the unmanaged approach many people suggested also has one problem, we
can not find a way to convert umanaged byte pointer to managed byte[].

At this time, I think the only way of doing this is copying memory which
you do not like.

I will do some research for this issue, I will reply to you as soon as I
research out.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Mon, 3 Nov 2003 16:57:53 -0500
| Lines: 115
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196416
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I do not like "bad code" although it works. So I want to make some thing
| clear. In your second approach, you will use a BlockCopy. After I call
this
| function, the system will COPY all the data from one place to another
place
| (so, we will get two copy of the data after it) or just give the reference
| to another array (so we have only one copy of data but have two refernce
of
| it.)? I need make this clear because I can not afford the copy in my
| application. I am working on a buffer size more the 2M, the COPY again and
| again will terribly reduce performance. And, in your first approach, I did
| not get the way to cast a pointer (byte*) back to byte[].
|
| "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
| message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > Ray,
| >
| > Your approach would work fine. The only reason I don't like it is
| > because it associates a copy of the array in question with the field.
| From
| > a design point of view, I would rather have two separate variables which
| > indicate that one is a different representation of the same thing.
| >
| > Also, I don't like having to define a new type explicitly for this
| > purpose, when there are methods to do so.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Ray Z" <su*********@hotmail.com> wrote in message
| > news:%2****************@tk2msftngp13.phx.gbl...
| > > Thanks.
| > > Then, what's wrong with my approach?
| > > In my case, I can not use copy for sure because I am talking about
| buffer
| > > size at least 2M bytes.
| > > About the unsafe code, how can I cast a byte* to a byte[]? I need
byte[]
| > > because the stream reader (I need read image data from file some
times)
| > need
| > > byte[] as it's parameter.
| > >
| > > Again, go back my approach, is there something wrong?
| > >
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > > > Ray,
| > > >
| > > > You have two options here. The first is to use unsafe code, and
| > then
| > > > cast the pointer to the byte array (which is really a pointer to a
| > > location
| > > > in memory to a byte) to a pointer to an integer, and then work from
| > there.
| > > >
| > > > If you want a purely managed solution, then I would use the
static
| > > > BlockCopy method on the Buffer class to copy the bytes from the byte
| > array
| > > > to an appropriately-sized integer array.
| > > >
| > > > Hope this helps.
| > > >
| > > >
| > > > --
| > > > - Nicholas Paldino [.NET/C# MVP]
| > > > - mv*@spam.guard.caspershouse.com
| > > >
| > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > > > > So far, I get the idea that if I want to use both the unmanaged
and
| > > > managed
| > > > > memory, I can not avoid memory copy. But I DO need to avoid it. I
| get
| > a
| > > > idea
| > > > > that maybe I could use "union" to convert byte[] to int[] and so
on.
| > > Here
| > > > is
| > > > > my source code, I wonder if this will work with GC?
| > > > >
| > > > > [StructLayout(LayoutKind.Explicit)]
| > > > >
| > > > > struct MyUnion
| > > > >
| > > > > {
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public int[] a;
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public byte[] b;
| > > > >
| > > > > }
| > > > >
| > > > > After I define this "union". I can use the following code to alloc
| and
| > > > > access the data buffer:
| > > > > MyUnion mu = new MyUnion();
| > > > > mu.a = new int[640*480];
| > > > > //if I like access it as byte[]
| > > > > byte[] bp = mu.b;
| > > > >
| > > > > will this work?
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #9
Hi Jeffrey, in regards to this, could you look at my post "Using
StructLayout causes app to end - no CLR exception" because it addresses (and
arguably 'fixes') the issue with fixed-length. In short - in isolation I
made the union suggestion work by using another unioned class to manipulate
the length. However, the result (when repeated in a for loop) is not at all
stable. Please have a look.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:dx*************@cpmsftngxa06.phx.gbl...

Hi Ray,

Thanks for posting in this group.
I have reviewed your post. Just as Ted points out, your approach of using
union is almost a good workaround, but it has some problem.
You can find the detailed problems in Ted's post.(It is mainly due to .Net
maintains the readonly length property of array).

While the unmanaged approach many people suggested also has one problem, we can not find a way to convert umanaged byte pointer to managed byte[].

At this time, I think the only way of doing this is copying memory which
you do not like.

I will do some research for this issue, I will reply to you as soon as I
research out.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Mon, 3 Nov 2003 16:57:53 -0500
| Lines: 115
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196416 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I do not like "bad code" although it works. So I want to make some thing
| clear. In your second approach, you will use a BlockCopy. After I call
this
| function, the system will COPY all the data from one place to another
place
| (so, we will get two copy of the data after it) or just give the reference | to another array (so we have only one copy of data but have two refernce
of
| it.)? I need make this clear because I can not afford the copy in my
| application. I am working on a buffer size more the 2M, the COPY again and | again will terribly reduce performance. And, in your first approach, I did | not get the way to cast a pointer (byte*) back to byte[].
|
| "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
| message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > Ray,
| >
| > Your approach would work fine. The only reason I don't like it is
| > because it associates a copy of the array in question with the field.
| From
| > a design point of view, I would rather have two separate variables which | > indicate that one is a different representation of the same thing.
| >
| > Also, I don't like having to define a new type explicitly for this
| > purpose, when there are methods to do so.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Ray Z" <su*********@hotmail.com> wrote in message
| > news:%2****************@tk2msftngp13.phx.gbl...
| > > Thanks.
| > > Then, what's wrong with my approach?
| > > In my case, I can not use copy for sure because I am talking about
| buffer
| > > size at least 2M bytes.
| > > About the unsafe code, how can I cast a byte* to a byte[]? I need
byte[]
| > > because the stream reader (I need read image data from file some
times)
| > need
| > > byte[] as it's parameter.
| > >
| > > Again, go back my approach, is there something wrong?
| > >
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > > > Ray,
| > > >
| > > > You have two options here. The first is to use unsafe code, and | > then
| > > > cast the pointer to the byte array (which is really a pointer to a
| > > location
| > > > in memory to a byte) to a pointer to an integer, and then work from | > there.
| > > >
| > > > If you want a purely managed solution, then I would use the
static
| > > > BlockCopy method on the Buffer class to copy the bytes from the byte | > array
| > > > to an appropriately-sized integer array.
| > > >
| > > > Hope this helps.
| > > >
| > > >
| > > > --
| > > > - Nicholas Paldino [.NET/C# MVP]
| > > > - mv*@spam.guard.caspershouse.com
| > > >
| > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > > > > So far, I get the idea that if I want to use both the unmanaged
and
| > > > managed
| > > > > memory, I can not avoid memory copy. But I DO need to avoid it. I | get
| > a
| > > > idea
| > > > > that maybe I could use "union" to convert byte[] to int[] and so
on.
| > > Here
| > > > is
| > > > > my source code, I wonder if this will work with GC?
| > > > >
| > > > > [StructLayout(LayoutKind.Explicit)]
| > > > >
| > > > > struct MyUnion
| > > > >
| > > > > {
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public int[] a;
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public byte[] b;
| > > > >
| > > > > }
| > > > >
| > > > > After I define this "union". I can use the following code to alloc | and
| > > > > access the data buffer:
| > > > > MyUnion mu = new MyUnion();
| > > > > mu.a = new int[640*480];
| > > > > //if I like access it as byte[]
| > > > > byte[] bp = mu.b;
| > > > >
| > > > > will this work?
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #10
Thanks, now I get a clear idea from you. I know the problem Teb point out.
He is right. And I know there is no way to conver a unmanaged pointer to a
managed array. If the COPY is the only way, I get no choice. So I need
carefully design my archtechture to avoid copy as best as I can.

When you find any solution, please let me know. I will keep watching this
topic.

Thanks for all guy.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:dx*************@cpmsftngxa06.phx.gbl...

Hi Ray,

Thanks for posting in this group.
I have reviewed your post. Just as Ted points out, your approach of using
union is almost a good workaround, but it has some problem.
You can find the detailed problems in Ted's post.(It is mainly due to .Net
maintains the readonly length property of array).

While the unmanaged approach many people suggested also has one problem, we can not find a way to convert umanaged byte pointer to managed byte[].

At this time, I think the only way of doing this is copying memory which
you do not like.

I will do some research for this issue, I will reply to you as soon as I
research out.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Mon, 3 Nov 2003 16:57:53 -0500
| Lines: 115
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196416 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I do not like "bad code" although it works. So I want to make some thing
| clear. In your second approach, you will use a BlockCopy. After I call
this
| function, the system will COPY all the data from one place to another
place
| (so, we will get two copy of the data after it) or just give the reference | to another array (so we have only one copy of data but have two refernce
of
| it.)? I need make this clear because I can not afford the copy in my
| application. I am working on a buffer size more the 2M, the COPY again and | again will terribly reduce performance. And, in your first approach, I did | not get the way to cast a pointer (byte*) back to byte[].
|
| "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
| message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > Ray,
| >
| > Your approach would work fine. The only reason I don't like it is
| > because it associates a copy of the array in question with the field.
| From
| > a design point of view, I would rather have two separate variables which | > indicate that one is a different representation of the same thing.
| >
| > Also, I don't like having to define a new type explicitly for this
| > purpose, when there are methods to do so.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Ray Z" <su*********@hotmail.com> wrote in message
| > news:%2****************@tk2msftngp13.phx.gbl...
| > > Thanks.
| > > Then, what's wrong with my approach?
| > > In my case, I can not use copy for sure because I am talking about
| buffer
| > > size at least 2M bytes.
| > > About the unsafe code, how can I cast a byte* to a byte[]? I need
byte[]
| > > because the stream reader (I need read image data from file some
times)
| > need
| > > byte[] as it's parameter.
| > >
| > > Again, go back my approach, is there something wrong?
| > >
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > > > Ray,
| > > >
| > > > You have two options here. The first is to use unsafe code, and | > then
| > > > cast the pointer to the byte array (which is really a pointer to a
| > > location
| > > > in memory to a byte) to a pointer to an integer, and then work from | > there.
| > > >
| > > > If you want a purely managed solution, then I would use the
static
| > > > BlockCopy method on the Buffer class to copy the bytes from the byte | > array
| > > > to an appropriately-sized integer array.
| > > >
| > > > Hope this helps.
| > > >
| > > >
| > > > --
| > > > - Nicholas Paldino [.NET/C# MVP]
| > > > - mv*@spam.guard.caspershouse.com
| > > >
| > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > > > > So far, I get the idea that if I want to use both the unmanaged
and
| > > > managed
| > > > > memory, I can not avoid memory copy. But I DO need to avoid it. I | get
| > a
| > > > idea
| > > > > that maybe I could use "union" to convert byte[] to int[] and so
on.
| > > Here
| > > > is
| > > > > my source code, I wonder if this will work with GC?
| > > > >
| > > > > [StructLayout(LayoutKind.Explicit)]
| > > > >
| > > > > struct MyUnion
| > > > >
| > > > > {
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public int[] a;
| > > > >
| > > > > [FieldOffset(0)]
| > > > >
| > > > > public byte[] b;
| > > > >
| > > > > }
| > > > >
| > > > > After I define this "union". I can use the following code to alloc | and
| > > > > access the data buffer:
| > > > > MyUnion mu = new MyUnion();
| > > > > mu.a = new int[640*480];
| > > > > //if I like access it as byte[]
| > > > > byte[] bp = mu.b;
| > > > >
| > > > > will this work?
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #11

Hi Ray,

Thanks for your feedaback.
I am still doing research now. I will reply to you ASAP.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
<#P**************@TK2MSFTNGP11.phx.gbl>
<dx*************@cpmsftngxa06.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Tue, 4 Nov 2003 10:23:31 -0500
| Lines: 197
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#t**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196610
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks, now I get a clear idea from you. I know the problem Teb point out.
| He is right. And I know there is no way to conver a unmanaged pointer to a
| managed array. If the COPY is the only way, I get no choice. So I need
| carefully design my archtechture to avoid copy as best as I can.
|
| When you find any solution, please let me know. I will keep watching this
| topic.
|
| Thanks for all guy.
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:dx*************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ray,
| >
| > Thanks for posting in this group.
| > I have reviewed your post. Just as Ted points out, your approach of
using
| > union is almost a good workaround, but it has some problem.
| > You can find the detailed problems in Ted's post.(It is mainly due to
Net
| > maintains the readonly length property of array).
| >
| > While the unmanaged approach many people suggested also has one problem,
| we
| > can not find a way to convert umanaged byte pointer to managed byte[].
| >
| > At this time, I think the only way of doing this is copying memory which
| > you do not like.
| >
| > I will do some research for this issue, I will reply to you as soon as I
| > research out.
| > Thanks for your understanding.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <su*********@hotmail.com>
| > | References: <uo**************@TK2MSFTNGP12.phx.gbl>
| > <us*************@TK2MSFTNGP11.phx.gbl>
| > <#F**************@tk2msftngp13.phx.gbl>
| > <uP**************@TK2MSFTNGP10.phx.gbl>
| > | Subject: Re: covert between byte[] and int []
| > | Date: Mon, 3 Nov 2003 16:57:53 -0500
| > | Lines: 115
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196416
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I do not like "bad code" although it works. So I want to make some
thing
| > | clear. In your second approach, you will use a BlockCopy. After I call
| > this
| > | function, the system will COPY all the data from one place to another
| > place
| > | (so, we will get two copy of the data after it) or just give the
| reference
| > | to another array (so we have only one copy of data but have two
refernce
| > of
| > | it.)? I need make this clear because I can not afford the copy in my
| > | application. I am working on a buffer size more the 2M, the COPY again
| and
| > | again will terribly reduce performance. And, in your first approach, I
| did
| > | not get the way to cast a pointer (byte*) back to byte[].
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > | message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > | > Ray,
| > | >
| > | > Your approach would work fine. The only reason I don't like it
is
| > | > because it associates a copy of the array in question with the
field.
| > | From
| > | > a design point of view, I would rather have two separate variables
| which
| > | > indicate that one is a different representation of the same thing.
| > | >
| > | > Also, I don't like having to define a new type explicitly for
this
| > | > purpose, when there are methods to do so.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - mv*@spam.guard.caspershouse.com
| > | >
| > | > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > news:%2****************@tk2msftngp13.phx.gbl...
| > | > > Thanks.
| > | > > Then, what's wrong with my approach?
| > | > > In my case, I can not use copy for sure because I am talking about
| > | buffer
| > | > > size at least 2M bytes.
| > | > > About the unsafe code, how can I cast a byte* to a byte[]? I need
| > byte[]
| > | > > because the stream reader (I need read image data from file some
| > times)
| > | > need
| > | > > byte[] as it's parameter.
| > | > >
| > | > > Again, go back my approach, is there something wrong?
| > | > >
| > | > >
| > | > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
| > wrote
| > | > in
| > | > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > | > > > Ray,
| > | > > >
| > | > > > You have two options here. The first is to use unsafe code,
| and
| > | > then
| > | > > > cast the pointer to the byte array (which is really a pointer
to a
| > | > > location
| > | > > > in memory to a byte) to a pointer to an integer, and then work
| from
| > | > there.
| > | > > >
| > | > > > If you want a purely managed solution, then I would use the
| > static
| > | > > > BlockCopy method on the Buffer class to copy the bytes from the
| byte
| > | > array
| > | > > > to an appropriately-sized integer array.
| > | > > >
| > | > > > Hope this helps.
| > | > > >
| > | > > >
| > | > > > --
| > | > > > - Nicholas Paldino [.NET/C# MVP]
| > | > > > - mv*@spam.guard.caspershouse.com
| > | > > >
| > | > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > | > > > > So far, I get the idea that if I want to use both the
unmanaged
| > and
| > | > > > managed
| > | > > > > memory, I can not avoid memory copy. But I DO need to avoid
it.
| I
| > | get
| > | > a
| > | > > > idea
| > | > > > > that maybe I could use "union" to convert byte[] to int[] and
so
| > on.
| > | > > Here
| > | > > > is
| > | > > > > my source code, I wonder if this will work with GC?
| > | > > > >
| > | > > > > [StructLayout(LayoutKind.Explicit)]
| > | > > > >
| > | > > > > struct MyUnion
| > | > > > >
| > | > > > > {
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public int[] a;
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public byte[] b;
| > | > > > >
| > | > > > > }
| > | > > > >
| > | > > > > After I define this "union". I can use the following code to
| alloc
| > | and
| > | > > > > access the data buffer:
| > | > > > > MyUnion mu = new MyUnion();
| > | > > > > mu.a = new int[640*480];
| > | > > > > //if I like access it as byte[]
| > | > > > > byte[] bp = mu.b;
| > | > > > >
| > | > > > > will this work?
| > | > > > >
| > | > > > >
| > | > > >
| > | > > >
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #12

Hi Ray,

So far as my research, I think copying data is the only way of handling
this issue. I think you can do some carefully copying, so that the
performance is better.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
<#P**************@TK2MSFTNGP11.phx.gbl>
<dx*************@cpmsftngxa06.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Tue, 4 Nov 2003 10:23:31 -0500
| Lines: 197
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#t**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196610
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks, now I get a clear idea from you. I know the problem Teb point out.
| He is right. And I know there is no way to conver a unmanaged pointer to a
| managed array. If the COPY is the only way, I get no choice. So I need
| carefully design my archtechture to avoid copy as best as I can.
|
| When you find any solution, please let me know. I will keep watching this
| topic.
|
| Thanks for all guy.
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:dx*************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ray,
| >
| > Thanks for posting in this group.
| > I have reviewed your post. Just as Ted points out, your approach of
using
| > union is almost a good workaround, but it has some problem.
| > You can find the detailed problems in Ted's post.(It is mainly due to
Net
| > maintains the readonly length property of array).
| >
| > While the unmanaged approach many people suggested also has one problem,
| we
| > can not find a way to convert umanaged byte pointer to managed byte[].
| >
| > At this time, I think the only way of doing this is copying memory which
| > you do not like.
| >
| > I will do some research for this issue, I will reply to you as soon as I
| > research out.
| > Thanks for your understanding.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <su*********@hotmail.com>
| > | References: <uo**************@TK2MSFTNGP12.phx.gbl>
| > <us*************@TK2MSFTNGP11.phx.gbl>
| > <#F**************@tk2msftngp13.phx.gbl>
| > <uP**************@TK2MSFTNGP10.phx.gbl>
| > | Subject: Re: covert between byte[] and int []
| > | Date: Mon, 3 Nov 2003 16:57:53 -0500
| > | Lines: 115
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196416
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I do not like "bad code" although it works. So I want to make some
thing
| > | clear. In your second approach, you will use a BlockCopy. After I call
| > this
| > | function, the system will COPY all the data from one place to another
| > place
| > | (so, we will get two copy of the data after it) or just give the
| reference
| > | to another array (so we have only one copy of data but have two
refernce
| > of
| > | it.)? I need make this clear because I can not afford the copy in my
| > | application. I am working on a buffer size more the 2M, the COPY again
| and
| > | again will terribly reduce performance. And, in your first approach, I
| did
| > | not get the way to cast a pointer (byte*) back to byte[].
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > | message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > | > Ray,
| > | >
| > | > Your approach would work fine. The only reason I don't like it
is
| > | > because it associates a copy of the array in question with the
field.
| > | From
| > | > a design point of view, I would rather have two separate variables
| which
| > | > indicate that one is a different representation of the same thing.
| > | >
| > | > Also, I don't like having to define a new type explicitly for
this
| > | > purpose, when there are methods to do so.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - mv*@spam.guard.caspershouse.com
| > | >
| > | > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > news:%2****************@tk2msftngp13.phx.gbl...
| > | > > Thanks.
| > | > > Then, what's wrong with my approach?
| > | > > In my case, I can not use copy for sure because I am talking about
| > | buffer
| > | > > size at least 2M bytes.
| > | > > About the unsafe code, how can I cast a byte* to a byte[]? I need
| > byte[]
| > | > > because the stream reader (I need read image data from file some
| > times)
| > | > need
| > | > > byte[] as it's parameter.
| > | > >
| > | > > Again, go back my approach, is there something wrong?
| > | > >
| > | > >
| > | > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
| > wrote
| > | > in
| > | > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > | > > > Ray,
| > | > > >
| > | > > > You have two options here. The first is to use unsafe code,
| and
| > | > then
| > | > > > cast the pointer to the byte array (which is really a pointer
to a
| > | > > location
| > | > > > in memory to a byte) to a pointer to an integer, and then work
| from
| > | > there.
| > | > > >
| > | > > > If you want a purely managed solution, then I would use the
| > static
| > | > > > BlockCopy method on the Buffer class to copy the bytes from the
| byte
| > | > array
| > | > > > to an appropriately-sized integer array.
| > | > > >
| > | > > > Hope this helps.
| > | > > >
| > | > > >
| > | > > > --
| > | > > > - Nicholas Paldino [.NET/C# MVP]
| > | > > > - mv*@spam.guard.caspershouse.com
| > | > > >
| > | > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > | > > > > So far, I get the idea that if I want to use both the
unmanaged
| > and
| > | > > > managed
| > | > > > > memory, I can not avoid memory copy. But I DO need to avoid
it.
| I
| > | get
| > | > a
| > | > > > idea
| > | > > > > that maybe I could use "union" to convert byte[] to int[] and
so
| > on.
| > | > > Here
| > | > > > is
| > | > > > > my source code, I wonder if this will work with GC?
| > | > > > >
| > | > > > > [StructLayout(LayoutKind.Explicit)]
| > | > > > >
| > | > > > > struct MyUnion
| > | > > > >
| > | > > > > {
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public int[] a;
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public byte[] b;
| > | > > > >
| > | > > > > }
| > | > > > >
| > | > > > > After I define this "union". I can use the following code to
| alloc
| > | and
| > | > > > > access the data buffer:
| > | > > > > MyUnion mu = new MyUnion();
| > | > > > > mu.a = new int[640*480];
| > | > > > > //if I like access it as byte[]
| > | > > > > byte[] bp = mu.b;
| > | > > > >
| > | > > > > will this work?
| > | > > > >
| > | > > > >
| > | > > >
| > | > > >
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #13
Thanks for your time.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:vJ**************@cpmsftngxa06.phx.gbl...

Hi Ray,

So far as my research, I think copying data is the only way of handling
this issue. I think you can do some carefully copying, so that the
performance is better.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ray Z" <su*********@hotmail.com>
| References: <uo**************@TK2MSFTNGP12.phx.gbl>
<us*************@TK2MSFTNGP11.phx.gbl>
<#F**************@tk2msftngp13.phx.gbl>
<uP**************@TK2MSFTNGP10.phx.gbl>
<#P**************@TK2MSFTNGP11.phx.gbl>
<dx*************@cpmsftngxa06.phx.gbl>
| Subject: Re: covert between byte[] and int []
| Date: Tue, 4 Nov 2003 10:23:31 -0500
| Lines: 197
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <#t**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196610 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks, now I get a clear idea from you. I know the problem Teb point out. | He is right. And I know there is no way to conver a unmanaged pointer to a | managed array. If the COPY is the only way, I get no choice. So I need
| carefully design my archtechture to avoid copy as best as I can.
|
| When you find any solution, please let me know. I will keep watching this | topic.
|
| Thanks for all guy.
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:dx*************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ray,
| >
| > Thanks for posting in this group.
| > I have reviewed your post. Just as Ted points out, your approach of
using
| > union is almost a good workaround, but it has some problem.
| > You can find the detailed problems in Ted's post.(It is mainly due to
Net
| > maintains the readonly length property of array).
| >
| > While the unmanaged approach many people suggested also has one problem, | we
| > can not find a way to convert umanaged byte pointer to managed byte[].
| >
| > At this time, I think the only way of doing this is copying memory which | > you do not like.
| >
| > I will do some research for this issue, I will reply to you as soon as I | > research out.
| > Thanks for your understanding.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Ray Z" <su*********@hotmail.com>
| > | References: <uo**************@TK2MSFTNGP12.phx.gbl>
| > <us*************@TK2MSFTNGP11.phx.gbl>
| > <#F**************@tk2msftngp13.phx.gbl>
| > <uP**************@TK2MSFTNGP10.phx.gbl>
| > | Subject: Re: covert between byte[] and int []
| > | Date: Mon, 3 Nov 2003 16:57:53 -0500
| > | Lines: 115
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#P**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: x42071bc2.ip.e-nt.net 66.7.27.194
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196416
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I do not like "bad code" although it works. So I want to make some
thing
| > | clear. In your second approach, you will use a BlockCopy. After I call | > this
| > | function, the system will COPY all the data from one place to another | > place
| > | (so, we will get two copy of the data after it) or just give the
| reference
| > | to another array (so we have only one copy of data but have two
refernce
| > of
| > | it.)? I need make this clear because I can not afford the copy in my
| > | application. I am working on a buffer size more the 2M, the COPY again | and
| > | again will terribly reduce performance. And, in your first approach, I | did
| > | not get the way to cast a pointer (byte*) back to byte[].
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| > in
| > | message news:uP**************@TK2MSFTNGP10.phx.gbl...
| > | > Ray,
| > | >
| > | > Your approach would work fine. The only reason I don't like it is
| > | > because it associates a copy of the array in question with the
field.
| > | From
| > | > a design point of view, I would rather have two separate variables
| which
| > | > indicate that one is a different representation of the same thing.
| > | >
| > | > Also, I don't like having to define a new type explicitly for
this
| > | > purpose, when there are methods to do so.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - mv*@spam.guard.caspershouse.com
| > | >
| > | > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > news:%2****************@tk2msftngp13.phx.gbl...
| > | > > Thanks.
| > | > > Then, what's wrong with my approach?
| > | > > In my case, I can not use copy for sure because I am talking about | > | buffer
| > | > > size at least 2M bytes.
| > | > > About the unsafe code, how can I cast a byte* to a byte[]? I need | > byte[]
| > | > > because the stream reader (I need read image data from file some
| > times)
| > | > need
| > | > > byte[] as it's parameter.
| > | > >
| > | > > Again, go back my approach, is there something wrong?
| > | > >
| > | > >
| > | > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> | > wrote
| > | > in
| > | > > message news:us*************@TK2MSFTNGP11.phx.gbl...
| > | > > > Ray,
| > | > > >
| > | > > > You have two options here. The first is to use unsafe code, | and
| > | > then
| > | > > > cast the pointer to the byte array (which is really a pointer
to a
| > | > > location
| > | > > > in memory to a byte) to a pointer to an integer, and then work
| from
| > | > there.
| > | > > >
| > | > > > If you want a purely managed solution, then I would use the | > static
| > | > > > BlockCopy method on the Buffer class to copy the bytes from the | byte
| > | > array
| > | > > > to an appropriately-sized integer array.
| > | > > >
| > | > > > Hope this helps.
| > | > > >
| > | > > >
| > | > > > --
| > | > > > - Nicholas Paldino [.NET/C# MVP]
| > | > > > - mv*@spam.guard.caspershouse.com
| > | > > >
| > | > > > "Ray Z" <su*********@hotmail.com> wrote in message
| > | > > > news:uo**************@TK2MSFTNGP12.phx.gbl...
| > | > > > > So far, I get the idea that if I want to use both the
unmanaged
| > and
| > | > > > managed
| > | > > > > memory, I can not avoid memory copy. But I DO need to avoid
it.
| I
| > | get
| > | > a
| > | > > > idea
| > | > > > > that maybe I could use "union" to convert byte[] to int[] and so
| > on.
| > | > > Here
| > | > > > is
| > | > > > > my source code, I wonder if this will work with GC?
| > | > > > >
| > | > > > > [StructLayout(LayoutKind.Explicit)]
| > | > > > >
| > | > > > > struct MyUnion
| > | > > > >
| > | > > > > {
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public int[] a;
| > | > > > >
| > | > > > > [FieldOffset(0)]
| > | > > > >
| > | > > > > public byte[] b;
| > | > > > >
| > | > > > > }
| > | > > > >
| > | > > > > After I define this "union". I can use the following code to
| alloc
| > | and
| > | > > > > access the data buffer:
| > | > > > > MyUnion mu = new MyUnion();
| > | > > > > mu.a = new int[640*480];
| > | > > > > //if I like access it as byte[]
| > | > > > > byte[] bp = mu.b;
| > | > > > >
| > | > > > > will this work?
| > | > > > >
| > | > > > >
| > | > > >
| > | > > >
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #14

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

Similar topics

3
by: Cindy Liu | last post by:
Hi Everyone, In .Net we have XmlSerializer to convert c# class to xml, or vice versa. Do we have any APIs to convert c# class to XSD? Thanks in advance, Cindy
2
by: Jim H | last post by:
I am getting binary data form a database binary(8). I am copying it to a byte array byte. I need to covert this to a number, ulong, so I can write it to a string in hex format. Any idea on how...
3
by: David | last post by:
I need to covert DateTime Variables into a byte array but all of the conversion methods do not appear to handle the DateTime type. Is there a generic way to convert any property or object into a...
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
6
by: CarlEOgden | last post by:
Hi, I've found this example but when I try to rework it for VB.Net I fail with the API calls and the CopyMemory procedure and to be truthful, it's far over my skill level with the getting...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
1
dmjpro
by: dmjpro | last post by:
i have an array of byte ... i want to convert the array into string. how can i do this? plz help. kind regards.
6
by: =?Utf-8?B?dmluY2VudA==?= | last post by:
Is there any method to covert a pdf file into jpg file in c#? Many thanks for your replying.
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.