473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

byte[] to int[]

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
Nov 16 '05 #1
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

Nov 16 '05 #2
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

Nov 16 '05 #3
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.
Nov 16 '05 #4
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


Nov 16 '05 #5
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.

Nov 16 '05 #6
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

Nov 16 '05 #7

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

Similar topics

2
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...
16
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...
13
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...
3
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 {...
1
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...
5
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
26
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...
6
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....
17
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...
24
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
0
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,...
0
Oralloy
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,...
0
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...
0
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...
0
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...
0
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...

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.