byte[] to int[]

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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
 
G

Guest

Thanks a million,
didn't know about this buffer class.

regards
brian

Nicholas Paldino said:
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]
- (e-mail address removed)

Brian Keating EI9FXB said:
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
 
G

Guest

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 said:
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
 

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

Top