Note that there are some explanatory texts on larger screens.

plurals
  1. POBit manipulation from a ByteArray
    primarykey
    data
    text
    <p>I would like to know how to manipulate bits from a ByteArray. What I need is to shift the bits according to a 'table' that I have.</p> <p>Table:</p> <blockquote> <pre><code>Bit 0 -&gt; Bit 26 Bit 1 -&gt; Bit 31 Bit 2 -&gt; Bit 17 ... Bit 31 -&gt; Bit 5 </code></pre> </blockquote> <p>I convert the ByteArray to BitArray using this method</p> <pre><code>public static BitArray ByteArraytoBitArray(byte[] bytes) { BitArray bits = new BitArray(bytes); return bits; } </code></pre> <p>But I am stuck there, I don't know how to shift the bits according to the table and then back to ByteArray.</p> <p><strong>EDIT:</strong></p> <p>Code Snipet:</p> <pre><code>public static byte[] StringToByteArray(String hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i &lt; NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); return bytes; } private void button3_Click(object sender, EventArgs e) { string featherearring = "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000"; var strarray = StringToByteArray(featherearring); byte[] strarray_comp = Enc.Encrypt(strarray); string conv = BitConverter.ToString(strarray_comp); MessageBox.Show(conv.Replace("-", "")); } public static byte[] BitArrayToByteArray(BitArray bits) { byte[] bytes = new byte[bits.Length / 8]; bits.CopyTo(bytes, 0); return bytes; } public static byte[] Encrypt(byte[] input) { BitArray source = new BitArray(input); BitArray target = new BitArray(source.Length); target[26] = source[0]; target[31] = source[1]; target[17] = source[2]; target[10] = source[3]; target[30] = source[4]; target[16] = source[5]; target[24] = source[6]; target[2] = source[7]; target[29] = source[8]; target[8] = source[9]; target[20] = source[10]; target[15] = source[11]; target[28] = source[12]; target[11] = source[13]; target[13] = source[14]; target[4] = source[15]; target[19] = source[16]; target[23] = source[17]; target[0] = source[18]; target[12] = source[19]; target[14] = source[20]; target[27] = source[21]; target[6] = source[22]; target[18] = source[23]; target[21] = source[24]; target[3] = source[25]; target[9] = source[26]; target[7] = source[27]; target[22] = source[28]; target[1] = source[29]; target[25] = source[30]; target[5] = source[31]; return BitArrayToByteArray(target); } </code></pre> <p>my input byte array is "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000" and my output with zimdanen's code is "5012000000000000000000000000000000000000000000000000000000000000" and it should be "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000" As you can see, it gets the first 2 bytes right, but the rest is are all null.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload