How do I treat a byte[] as long[]

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a byte[] how would I port code from C++ to manipulate the byte
array as if it were a long[]?

For example:

// C++
// assume the following
// unsigned char* m_pPalette;
// unsigned char* m_pData;
//
DWORD* pDest= (DWORD*)m_pData;
DWORD* pSrc = (DWORD*)m_pPalette;
int i;
for (i = 0; i < iLenData; i++)
pDest = pSrc;

// C#
// assume
// byte[] pData
// byte[] m_pModifiedData
// byte[] m_pPalette
//
unsafe
{
fixed(byte* dest = m_pModifiedData, src = m_pPalette)
{
int i;
for(i=0;i<length;i++)
(long[])dest = (long[])src[pData];
}
}


I'm trying to manipulate the memory directly to the m_pModifiedData without
having to do a System.Buffer.BlockCopy to another array and then back again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis
 
Not sure what your intent is, but does this help:

byte[] array = new byte[] { 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2 };
byte[] array2 = new byte[16];

unsafe
{
fixed ( byte* dest = array2, src = array )
{
long* ps = (long*)src; // Cast a byte* to a long*.
long* pd = (long*)dest;

for ( int i = 0; i < array.Length; i += 8 )
{
*pd = *ps; // Assignment.
pd++; // Move 8 bytes as we are a long ptr.
ps++;
}
}
}
foreach ( byte b in array2 )
{
Console.WriteLine(b.ToString());
}
Console.WriteLine("Done");
 
Thank you. This is what I was looking for. Sorry about not presenting my
intent more clearly, but this is exactly right.

I need to manipulate a byte[] 4 bytes at a time as if it were a long* in
C++. The only thing is I think I should be using Int32* instead of a long*
in C#.

Thanks again.


William Stacey said:
Not sure what your intent is, but does this help:

byte[] array = new byte[] { 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2 };
byte[] array2 = new byte[16];

unsafe
{
fixed ( byte* dest = array2, src = array )
{
long* ps = (long*)src; // Cast a byte* to a long*.
long* pd = (long*)dest;

for ( int i = 0; i < array.Length; i += 8 )
{
*pd = *ps; // Assignment.
pd++; // Move 8 bytes as we are a long ptr.
ps++;
}
}
}
foreach ( byte b in array2 )
{
Console.WriteLine(b.ToString());
}
Console.WriteLine("Done");

--
William Stacey [MVP]

Dennis said:
If I have a byte[] how would I port code from C++ to manipulate the byte
array as if it were a long[]?

For example:

// C++
// assume the following
// unsigned char* m_pPalette;
// unsigned char* m_pData;
//
DWORD* pDest= (DWORD*)m_pData;
DWORD* pSrc = (DWORD*)m_pPalette;
int i;
for (i = 0; i < iLenData; i++)
pDest = pSrc;

// C#
// assume
// byte[] pData
// byte[] m_pModifiedData
// byte[] m_pPalette
//
unsafe
{
fixed(byte* dest = m_pModifiedData, src = m_pPalette)
{
int i;
for(i=0;i<length;i++)
(long[])dest = (long[])src[pData];
}
}


I'm trying to manipulate the memory directly to the m_pModifiedData
without
having to do a System.Buffer.BlockCopy to another array and then back
again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis

 
A long in .Net is int64 or 8 bytes. If you want 4 bytes, then use int
(int32). Glad it helps.

--
William Stacey [MVP]

Dennis said:
Thank you. This is what I was looking for. Sorry about not presenting my
intent more clearly, but this is exactly right.

I need to manipulate a byte[] 4 bytes at a time as if it were a long* in
C++. The only thing is I think I should be using Int32* instead of a
long*
in C#.

Thanks again.


William Stacey said:
Not sure what your intent is, but does this help:

byte[] array = new byte[] {
1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2 };
byte[] array2 = new byte[16];

unsafe
{
fixed ( byte* dest = array2, src = array )
{
long* ps = (long*)src; // Cast a byte* to a long*.
long* pd = (long*)dest;

for ( int i = 0; i < array.Length; i += 8 )
{
*pd = *ps; // Assignment.
pd++; // Move 8 bytes as we are a long ptr.
ps++;
}
}
}
foreach ( byte b in array2 )
{
Console.WriteLine(b.ToString());
}
Console.WriteLine("Done");

--
William Stacey [MVP]

Dennis said:
If I have a byte[] how would I port code from C++ to manipulate the
byte
array as if it were a long[]?

For example:

// C++
// assume the following
// unsigned char* m_pPalette;
// unsigned char* m_pData;
//
DWORD* pDest= (DWORD*)m_pData;
DWORD* pSrc = (DWORD*)m_pPalette;
int i;
for (i = 0; i < iLenData; i++)
pDest = pSrc;

// C#
// assume
// byte[] pData
// byte[] m_pModifiedData
// byte[] m_pPalette
//
unsafe
{
fixed(byte* dest = m_pModifiedData, src = m_pPalette)
{
int i;
for(i=0;i<length;i++)
(long[])dest = (long[])src[pData];
}
}


I'm trying to manipulate the memory directly to the m_pModifiedData
without
having to do a System.Buffer.BlockCopy to another array and then back
again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top