473,738 Members | 11,146 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 2134
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
3271
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 this?
19
4149
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
22
3719
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 did support "long double", but theirs was an 8-byte or 10-byte or 12-byte type with accuracy the...
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 re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
6
2782
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 the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
4
3397
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 error tells me something like "Additional information: Caractère de substitution faible trouvé...
4
2214
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 compile-time. The code potentially invokes undefined behaviour if an unsigned integer contains padding bits....
5
16144
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
3425
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 gif image that is returned by this code in an object. Here is the code: GifEncoder.Class.php
0
8969
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.