473,562 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integers to Bit Array


I need to pack two (or more) ints into a byte []. I know I can use
BitConverter to to convert one int into a byte [], but how do I do more -
and how do I get them out again?

byte [] ToByteArray(int a, int b)
{
return ???;
}

void FromByteArray(b yte [] byteArray, out int a, out int b)
{
???
}

Thanks,

Andrew
Feb 20 '06 #1
4 2736
amaca wrote:
I need to pack two (or more) ints into a byte []. I know I can use
BitConverter to to convert one int into a byte [], but how do I do more -
and how do I get them out again?

byte [] ToByteArray(int a, int b)
{
return ???;
}

void FromByteArray(b yte [] byteArray, out int a, out int b)
{
???
}


Unfortunately the normal BitConverter doesn't help you as much as it
could in terms of putting them together.

I have a version of BitConverter in my miscellaneous utility library
which not only allows you to specify the endianness, but also has a
CopyBytes method (IIRC - something like that). It's like GetBytes, but
it lets you specify an existing byte array and the index at which to
copy the bytes.

FromByteArray is reasonable with the normal BitConverter though - just
use:
a = BitConverter.To Int32(byteArray , 0);
b = BitConverter.To Int32(byteArray , 4);

My library is available at
http://www.pobox.com/~skeet/csharp/miscutil

Jon

Feb 20 '06 #2
Many thanks - just what I need. As is often the case, the problem was indeed
'something that doesn't exist in the framework but {we/I} think it should'.

Andrew
Feb 20 '06 #3
The Buffer class may be useful.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio graphics editor

"amaca" <pu****@ajam.ne t> wrote in message
news:e%******** ********@TK2MSF TNGP09.phx.gbl. ..

I need to pack two (or more) ints into a byte []. I know I can use
BitConverter to to convert one int into a byte [], but how do I do more -
and how do I get them out again?

byte [] ToByteArray(int a, int b)
{
return ???;
}

void FromByteArray(b yte [] byteArray, out int a, out int b)
{
???
}

Thanks,

Andrew

Feb 20 '06 #4
It might indeed be, thank you.

Andrew
Feb 21 '06 #5

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

Similar topics

29
7448
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the function will return me an array holding a random subset of integers in that range of a size that I specify So the Function would Probabaly look...
13
2521
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of these, and am interested to know whether the storage efficiency of long integers is in danger of breaking my code if I use too many. Would I do better...
49
2143
by: David | last post by:
I need to do an array with around 15000 integers in it. I have declared it as unsigned letter; But the compiler is saying that this is too much. How can I create an array for 15000 integers? I am using "Miracle C compiler". This is the error i am recieving: "c:\documents and settings\david\desktop\asdasdasdasdasdasdasdad22.c:
3
21924
by: Jeff | last post by:
....still new to vb.net 2005 I understand the concept of arrays, and have used them in other languages, but was hoping that someone could get me started with something. I have a fairly long list of values that start in textboxes and listboxes that are both integers and strings. I need to store them in an array to pass to subs and stored...
13
10380
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able...
0
7652
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...
0
8095
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7625
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...
1
5476
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...
0
5193
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3621
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...
0
3606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2073
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
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.