byte[] to byte*... What is the fastest way?

This is a discussion on byte[] to byte*... What is the fastest way? within the Framework and Interface Programming forums in category; Hi, The subject says it all... I want to use a byte[] and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic...

Go Back   Application Development Forum > Framework and Interface Programming

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-23-2007, 03:36 PM
ThunderMusic
Guest
 
Default byte[] to byte*... What is the fastest way?

Hi,
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Thanks

ThunderMusic


Reply With Quote
  #2  
Old 08-23-2007, 03:51 PM
Mattias Sjögren
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

>The subject says it all... I want to use a byte[] and use it as byte* so I
>can increment the pointer to iterate through it.
>
>What is the fastest way of doing so in C#?


The fixed statement?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply With Quote
  #3  
Old 08-23-2007, 03:51 PM
Mattias Sjögren
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

>The subject says it all... I want to use a byte[] and use it as byte* so I
>can increment the pointer to iterate through it.
>
>What is the fastest way of doing so in C#?


The fixed statement?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply With Quote
  #4  
Old 08-23-2007, 03:54 PM
Nicholas Paldino [.NET/C# MVP]
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
news:%23KkK5zb5HHA.5316@TK2MSFTNGP04.phx.gbl...
> Hi,
> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.
>
> What is the fastest way of doing so in C#?
>
> Thanks
>
> ThunderMusic
>



Reply With Quote
  #5  
Old 08-23-2007, 03:54 PM
Nicholas Paldino [.NET/C# MVP]
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
news:%23KkK5zb5HHA.5316@TK2MSFTNGP04.phx.gbl...
> Hi,
> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.
>
> What is the fastest way of doing so in C#?
>
> Thanks
>
> ThunderMusic
>



Reply With Quote
  #6  
Old 08-23-2007, 04:02 PM
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

Hi,

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
news:%23KkK5zb5HHA.5316@TK2MSFTNGP04.phx.gbl...
> Hi,
> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.
>
> What is the fastest way of doing so in C#?


Out of curiosity, why you want to do that?

What is wrong with using a indexer like
for( int i=0; i< buffer.Lengh; i++
buffer[i] .....



Reply With Quote
  #7  
Old 08-23-2007, 04:02 PM
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

Hi,

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
news:%23KkK5zb5HHA.5316@TK2MSFTNGP04.phx.gbl...
> Hi,
> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.
>
> What is the fastest way of doing so in C#?


Out of curiosity, why you want to do that?

What is wrong with using a indexer like
for( int i=0; i< buffer.Lengh; i++
buffer[i] .....



Reply With Quote
  #8  
Old 08-23-2007, 04:05 PM
Chris Mullins [MVP]
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote:

> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.


I really hate to be pedantic, but I'm willing to bet that the difference in
how you iterate through your array makes little to no difference in the
overall performance of your code.

People frequently are guilting of over-optimizing things that are already
"Fast Enough". Unless you've verified this section is slow via a Profiler,
you're better off not getting fancy with optimizations.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins


Reply With Quote
  #9  
Old 08-23-2007, 04:05 PM
Chris Mullins [MVP]
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote:

> The subject says it all... I want to use a byte[] and use it as byte* so I
> can increment the pointer to iterate through it.


I really hate to be pedantic, but I'm willing to bet that the difference in
how you iterate through your array makes little to no difference in the
overall performance of your code.

People frequently are guilting of over-optimizing things that are already
"Fast Enough". Unless you've verified this section is slow via a Profiler,
you're better off not getting fancy with optimizations.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins


Reply With Quote
  #10  
Old 08-23-2007, 04:06 PM
ThunderMusic
Guest
 
Default Re: byte[] to byte*... What is the fastest way?

hi,
thanks for the really quick answer... Your solution works, but I can't
do p++ because it's fixed. So I must use another pointer like byte* p2 = p;
so now I can do p2++;...

Actually, I'm looking for a faster way to compute a checksum on a byte
array... For now, I'm using the Adler-32 algorithm, but I'm open to advises
on a performant checksum algorithm. It will be for an error checking
mecanism for tcp and udp communication on a closed network environment. So
it doesn't need to be human-modification resistant, it's just to prevent
modification due to the noise on the line (if it can happen)...

Thanks

ThunderMusic

"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:uezij7b5HHA.5360@TK2MSFTNGP03.phx.gbl...
> ThunderMusic,
>
> Use unsafe code:
>
> byte[] bytes = ...;
>
> unsafe
> {
> fixed (byte* p = bytes)
> {
> // Work with pointer here.
> }
> }
>
> As a matter of fact, that's the only way to do it, as you need to pin
> down the location of the array to prevent the reference from moving
> around.
>
> Is there a reason you need the pointer, or are you just looking for a
> faster way to iterate through the array?
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
> news:%23KkK5zb5HHA.5316@TK2MSFTNGP04.phx.gbl...
>> Hi,
>> The subject says it all... I want to use a byte[] and use it as byte* so
>> I can increment the pointer to iterate through it.
>>
>> What is the fastest way of doing so in C#?
>>
>> Thanks
>>
>> ThunderMusic
>>

>
>



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:33 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.