473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_pPale tte;
int i;
for (i = 0; i < iLenData; i++)
pDest[i] = pSrc[i];

// 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<lengt h;i++)
(long[])dest[i] = (long[])src[pData[i]];
}
}
I'm trying to manipulate the memory directly to the m_pModifiedData without
having to do a System.Buffer.B lockCopy to another array and then back again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis
Nov 17 '05 #1
3 2113
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.WriteLi ne(b.ToString() );
}
Console.WriteLi ne("Done");

--
William Stacey [MVP]

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:19******** *************** ***********@mic rosoft.com...
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_pPale tte;
int i;
for (i = 0; i < iLenData; i++)
pDest[i] = pSrc[i];

// 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<lengt h;i++)
(long[])dest[i] = (long[])src[pData[i]];
}
}
I'm trying to manipulate the memory directly to the m_pModifiedData
without
having to do a System.Buffer.B lockCopy to another array and then back
again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis

Nov 17 '05 #2
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 [MVP]" wrote:
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.WriteLi ne(b.ToString() );
}
Console.WriteLi ne("Done");

--
William Stacey [MVP]

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:19******** *************** ***********@mic rosoft.com...
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_pPale tte;
int i;
for (i = 0; i < iLenData; i++)
pDest[i] = pSrc[i];

// 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<lengt h;i++)
(long[])dest[i] = (long[])src[pData[i]];
}
}
I'm trying to manipulate the memory directly to the m_pModifiedData
without
having to do a System.Buffer.B lockCopy to another array and then back
again
in my member variable after the manipulation.

Is this possible?

Thanks

Dennis


Nov 17 '05 #3
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" <De****@discuss ions.microsoft. com> wrote in message
news:14******** *************** ***********@mic rosoft.com...
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 [MVP]" wrote:
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.WriteLi ne(b.ToString() );
}
Console.WriteLi ne("Done");

--
William Stacey [MVP]

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:19******** *************** ***********@mic rosoft.com...
> 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_pPale tte;
> int i;
> for (i = 0; i < iLenData; i++)
> pDest[i] = pSrc[i];
>
> // 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<lengt h;i++)
> (long[])dest[i] = (long[])src[pData[i]];
> }
> }
>
>
> I'm trying to manipulate the memory directly to the m_pModifiedData
> without
> having to do a System.Buffer.B lockCopy to another array and then back
> again
> in my member variable after the manipulation.
>
> Is this possible?
>
> Thanks
>
> Dennis


Nov 17 '05 #4

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

Similar topics

9
3249
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte a={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0} and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which should then contain 43921662 how to achieve...
19
4113
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
22
3682
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What wintel compilers support a 16-byte "long double" (with 33 decimal digits of accuracy) including proper printf() support. I found some compilers that...
289
1805
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be...
6
2750
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below...
4
3385
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an...
4
2206
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1, and so on. The code computes at runtime the byte-order of the unsigned integer, but alas it would be better if it could be determined at...
5
16127
jeffbroodwar
by: jeffbroodwar | last post by:
Hi everyone, I have a program that converts variables long,string,double to byte array here's the code : for long : //CompanyId temp = longToByteArray(CompanyId); for (i=0,i2=7; i<5; i++,i2--)
5
3405
by: helraizer1 | last post by:
Me again :D You're going to be sick of me soon. =P I found the GifEncoder class on phpclasses, which creates an animated gif image from frames and echos it to the browser to show; can also save it as an image with .gif extension. The only problem is that I need to add the animated gif onto another image (with imagecopymerge), however the...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.