473,397 Members | 2,077 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,397 software developers and data experts.

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(byte [] byteArray, out int a, out int b)
{
???
}

Thanks,

Andrew
Feb 20 '06 #1
4 2729
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(byte [] 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.ToInt32(byteArray, 0);
b = BitConverter.ToInt32(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.net> wrote in message
news:e%****************@TK2MSFTNGP09.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(byte [] 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
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...
13
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...
49
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...
3
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...
13
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...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
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.