473,698 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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[] byteViewColWidt hs = (Byte[])objViewColWidt hs;
m_nNumCols = byteViewColWidt hs.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.To Int32(byteViewC olWidths, nIdx);

basically convert every 4 bytes to an interger.

Is this best approach?
thanks in advance
Brian
Nov 16 '05 #1
6 4900
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[byteViewColWidt hs.Length/4];
for(int intIndex=0; intIndex<m_arrC olWidths.Length ; intIndex++) {
m_arrColWidths[intIndex]=BitConverter.T oInt32(byteView ColWidths
, 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[] byteViewColWidt hs = (Byte[])objViewColWidt hs;
m_nNumCols = byteViewColWidt hs.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.To Int32(byteViewC olWidths, 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.co m

"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:55******** *************** ***********@mic rosoft.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[] byteViewColWidt hs = (Byte[])objViewColWidt hs;
m_nNumCols = byteViewColWidt hs.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.To Int32(byteViewC olWidths,
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.BlockCop y 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.co m

"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:55******** *************** ***********@mic rosoft.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[] byteViewColWidt hs = (Byte[])objViewColWidt hs;
m_nNumCols = byteViewColWidt hs.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.To Int32(byteViewC olWidths,
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.BlockCop y 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[byteViewColWidt hs.Length/4];
for(int intIndex=0; intIndex<m_arrC olWidths.Length ; intIndex++) {
m_arrColWidths[intIndex]=BitConverter.T oInt32(byteView ColWidths
, 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[] byteViewColWidt hs = (Byte[])objViewColWidt hs;
m_nNumCols = byteViewColWidt hs.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.To Int32(byteViewC olWidths, 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
26907
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 IPv4 address. Ok, yes, in theory, there are enough bits to hold the values. But, my Java book clearly states that a byte is a SIGNED quantity, is part of the Integer class, and can hold values ranging from 127
16
9216
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 of an int is 4 bytes. How is it that a char then gets 1 byte. Shouldn't it also get 4 bytes even though it might be able to store only 256 values? Is the OS doing some sort of trimming? Thanks
13
2156
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 byte to int and so on. Here is my source code, I wonder if this will work with GC? struct MyUnion {
3
1885
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 { byte pB = new byte;
1
32863
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 = (int) dataRow;
5
1722
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
11551
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 partner web service has a 20 byte limit on transaction ID passed.
6
2185
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. So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
17
7248
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 to show the array data to the end user. Can I do that? How?
24
2287
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
8683
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...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8902
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
8873
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
7740
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
6528
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
4372
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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.