Convert Int to a Bit Array in .Net

Convert int to a bit array in .NET

To convert the int 'x'

int x = 3;

One way, by manipulation on the int :

string s = Convert.ToString(x, 2); //Convert to binary in a string

int[] bits= s.PadLeft(8, '0') // Add 0's from left
.Select(c => int.Parse(c.ToString())) // convert each char to int
.ToArray(); // Convert IEnumerable from select to Array

Alternatively, by using the BitArray class-

BitArray b = new BitArray(new byte[] { x });
int[] bits = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();

How to convert an integer to binary stored in array

You can add some Linq to represent the string as an array:

string source = Convert.ToString(number, toBase).PadLeft(length, '0');

...

int[] binary = source.Select(c => c - '0').ToArray();

...

bool[] binary = source.Select(c => c == '1').ToArray();

Or you can compute arrays directly:

int[] binary = Enumerable
.Range(1, length)
.Select(i => number / (1 << (length - i)) % 2)
.ToArray();

bool[] binary = Enumerable
.Range(1, length)
.Select(i => number / (1 << (length - i)) % 2 == 1)
.ToArray();

Initialize BitArray from integer

You are looping from the wrong direction. Try this:

    var b = new BitArray(BitConverter.GetBytes(0xfa2));
for (int i = b.Count-1; i >= 0; i--)
{
char c = b[i] ? '1' : '0';
Console.Write(c);
}
Console.WriteLine();

Creating Bit Array in Powershell/C# from integers

I am not sure what you want to achieve. 5bits words are literally odd.
It could be that there is no clear conversion here but something like a hash. Anyways, you could technically count from 0 to 2^35 - 1 and poke that in your game and lookup the result in your database.

Let me give you a few conversion methods:

To bit array:

$Bits = 
[convert]::ToString(192, 2).PadLeft(15, '0') +
[convert]::ToString( 20, 2).PadLeft(10, '0') +
[convert]::ToString( 19, 2).PadLeft( 5, '0') +
[convert]::ToString( 2, 2).PadLeft( 5, '0')

$Bits
00000001100000000000101001001100010

And back:

if ($Bits -Match '(.{15})(.{10})(.{5})(.{5})') {
$Matches[1..4].Foreach{ [convert]::ToByte($_, 2) }
}
192
20
19
2

To Int64:

$Int64 = [convert]::ToInt64($Bits, 2)

$Int64
201347682

To bytes:

$Bytes = [BitConverter]::GetBytes($Int64)

[System.BitConverter]::ToString($Bytes)
62-52-00-0C-00-00-00-00

Note that the bytes list is reverse order:

[convert]::ToString(0x62, 2)
1100010

Convert integer to binary in C#

Your example has an integer expressed as a string. Let's say your integer was actually an integer, and you want to take the integer and convert it to a binary string.

int value = 8;
string binary = Convert.ToString(value, 2);

Which returns 1000.

Convert integers to a Byte array VB.net

You can convert an integer (32 bit (4 byte)) to a byte array using the BitConverter class.

Dim result As Byte() = BitConverter.GetBytes(-95I)

Dim b1 As Byte = result(0) '161
Dim b2 As Byte = result(1) '255
Dim b3 As Byte = result(2) '255
Dim b4 As Byte = result(3) '255

How do I convert a int to an array of byte's and then back?

Try

BitConverter.GetBytes()

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

Just keep in mind that the order of the bytes in returned array depends on the endianness of your system.

EDIT:
As for the Lua part, I don't know how to convert back. You could always multiply by 16 to get the same functionality of a bitwise shift by 4. It's not pretty and I would imagine there is some library or something that implements it. Again, the order to add the bytes in depends on the endianness, so you might want to read up on that

Maybe you can convert back in C#?

Initialize BitArray from integer

You are looping from the wrong direction. Try this:

    var b = new BitArray(BitConverter.GetBytes(0xfa2));
for (int i = b.Count-1; i >= 0; i--)
{
char c = b[i] ? '1' : '0';
Console.Write(c);
}
Console.WriteLine();


Related Topics



Leave a reply



Submit