Hello there,
I've just come accross this and wonder if i'm taking the best approach?
I read a byte[] array from the registry, this byte array is basically an
array of Int32 so i wish to revert and store this Int32 array,
I do so as follows
Byte[] byteViewColWidths = (Byte[])objViewColWidths;
m_nNumCols = byteViewColWidths.Length / Marshal.SizeOf(typeof(Int32));
m_arrColWidths = new Int32[m_nNumCols];
int nIntOffset = 0;
for (int nIdx = 0; nIdx < m_nNumCols; nIdx = nIdx + 4, ++nIntOffset)
m_arrColWidths[nIntOffset] = BitConverter.ToInt32(byteViewColWidths, nIdx);
basically convert every 4 bytes to an interger.
Is this best approach?
thanks in advance
Brian 6 4886
Hi Brian,
I see that "nIdx" defines index in array-of-bytes so i don't like
"nIdx < m_nNumCols" comparison.
// if you're sure that byte's array contains only int32s and their
// byte-order is compatible with your processor then you can use it:
int[] m_arrColWidths = new int[byteViewColWidths.Length/4];
for(int intIndex=0; intIndex<m_arrColWidths.Length; intIndex++) {
m_arrColWidths[intIndex]=BitConverter.ToInt32(byteViewColWidths
, 4*intIndex);
}
HTH
Marcin Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte[] array from the registry, this byte array is basically an array of Int32 so i wish to revert and store this Int32 array,
I do so as follows Byte[] byteViewColWidths = (Byte[])objViewColWidths; m_nNumCols = byteViewColWidths.Length / Marshal.SizeOf(typeof(Int32)); m_arrColWidths = new Int32[m_nNumCols]; int nIntOffset = 0; for (int nIdx = 0; nIdx < m_nNumCols; nIdx = nIdx + 4, ++nIntOffset) m_arrColWidths[nIntOffset] = BitConverter.ToInt32(byteViewColWidths, nIdx);
basically convert every 4 bytes to an interger.
Is this best approach? thanks in advance Brian
Brian,
You have two options here. The first is to use unsafe code. In unsafe
code, you can take the pointer to the array of bytes, and cast it to an
array of integers, and you can access it normally.
The second would be to call the BlockCopy method on the Buffer class,
which will allow you to copy the byte array into the integer array.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message
news:55**********************************@microsof t.com... Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte[] array from the registry, this byte array is basically an array of Int32 so i wish to revert and store this Int32 array,
I do so as follows Byte[] byteViewColWidths = (Byte[])objViewColWidths; m_nNumCols = byteViewColWidths.Length / Marshal.SizeOf(typeof(Int32)); m_arrColWidths = new Int32[m_nNumCols]; int nIntOffset = 0; for (int nIdx = 0; nIdx < m_nNumCols; nIdx = nIdx + 4, ++nIntOffset) m_arrColWidths[nIntOffset] = BitConverter.ToInt32(byteViewColWidths, nIdx);
basically convert every 4 bytes to an interger.
Is this best approach? thanks in advance Brian
Thanks a million,
didn't know about this buffer class.
regards
brian
"Nicholas Paldino [.NET/C# MVP]" wrote: Brian,
You have two options here. The first is to use unsafe code. In unsafe code, you can take the pointer to the array of bytes, and cast it to an array of integers, and you can access it normally.
The second would be to call the BlockCopy method on the Buffer class, which will allow you to copy the byte array into the integer array.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message news:55**********************************@microsof t.com... Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte[] array from the registry, this byte array is basically an array of Int32 so i wish to revert and store this Int32 array,
I do so as follows Byte[] byteViewColWidths = (Byte[])objViewColWidths; m_nNumCols = byteViewColWidths.Length / Marshal.SizeOf(typeof(Int32)); m_arrColWidths = new Int32[m_nNumCols]; int nIntOffset = 0; for (int nIdx = 0; nIdx < m_nNumCols; nIdx = nIdx + 4, ++nIntOffset) m_arrColWidths[nIntOffset] = BitConverter.ToInt32(byteViewColWidths, nIdx);
basically convert every 4 bytes to an interger.
Is this best approach? thanks in advance Brian
Thanks a million,
didn't know about this buffer class.
regards
brian
"Mattias Sjögren" wrote: Brian,
Is this best approach?
I'd use Buffer.BlockCopy instead. Mattias
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
you're quite right,
had only typed in and never got around to the testing phase.
I'm gonna go with the Buffer class option however, i didn't know about this
class.
thanks for you reply
regards
Brian
"Marcin Grzębski" wrote: Hi Brian,
I see that "nIdx" defines index in array-of-bytes so i don't like "nIdx < m_nNumCols" comparison.
// if you're sure that byte's array contains only int32s and their // byte-order is compatible with your processor then you can use it:
int[] m_arrColWidths = new int[byteViewColWidths.Length/4]; for(int intIndex=0; intIndex<m_arrColWidths.Length; intIndex++) { m_arrColWidths[intIndex]=BitConverter.ToInt32(byteViewColWidths , 4*intIndex); }
HTH Marcin
Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte[] array from the registry, this byte array is basically an array of Int32 so i wish to revert and store this Int32 array,
I do so as follows Byte[] byteViewColWidths = (Byte[])objViewColWidths; m_nNumCols = byteViewColWidths.Length / Marshal.SizeOf(typeof(Int32)); m_arrColWidths = new Int32[m_nNumCols]; int nIntOffset = 0; for (int nIdx = 0; nIdx < m_nNumCols; nIdx = nIdx + 4, ++nIntOffset) m_arrColWidths[nIntOffset] = BitConverter.ToInt32(byteViewColWidths, nIdx);
basically convert every 4 bytes to an interger.
Is this best approach? thanks in advance Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Cook |
last post by:
Java's InetAddress class has some methods that use
a byte-array to hold what it describes as a 'raw IP address'.
So, I assume that they mean an array like:
byte ba = new byte;
would hold an...
|
by: Samuel Thomas |
last post by:
Hello Friends,
I understand(could be wrong) that the smallest chunk of memory is
called a word. If that is correct, that means if I am using a 32 bit
OS a word is 4 bytes. So that's why the size...
|
by: Ray Z |
last post by:
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...
|
by: Ted Miller |
last post by:
Hi folks,
I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an
array of 3 pointers to bytes, typed as byte**.
public static extern void foo(byte **p3pb);
unsafe {...
|
by: Wasim Akram |
last post by:
Hi,
I have a field "Month" in my SQL server table. The type of this field is
"tinyint".
Now what I am doing in the code is using DataRow to read this field in a
'int' variable.
int month...
|
by: Gianmaria I. |
last post by:
Hi,
having a BitArray, how can i extract bits to create a System.byte
as in the example...
With
BitArray bits
and
Byte myNewByte
|
by: John Grandy |
last post by:
Is it possible to generate a 20 byte integer from a GUID that is "unique
enough" ( just like a GUID is not truly unique , but is "unique enough" ).
We identify transactions with GUIDs , but a...
|
by: lovecreatesbeauty |
last post by:
/*
It seems that when an int with width of four bytes is assigned to a one
byte width char, the first three bytes from left to right are discarded
and the rightest byte is assigned to that char....
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: ThunderMusic |
last post by:
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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...
|
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...
| |