Note that there are some explanatory texts on larger screens.

plurals
  1. PORead file into ByteArrays of 4 bytes
    primarykey
    data
    text
    <p>I would like to know how I could read a file into ByteArrays that are 4 bytes long. These arrays will be manipulated and then have to be converted back to a single array ready to be written to a file.</p> <p><strong>EDIT:</strong> Code snippet.</p> <pre><code> var arrays = new List&lt;byte[]&gt;(); using (var f = new FileStream("file.cfg.dec", FileMode.Open)) { for (int i = 0; i &lt; f.Length; i += 4) { var b = new byte[4]; var bytesRead = f.Read(b, i, 4); if (bytesRead &lt; 4) { var b2 = new byte[bytesRead]; Array.Copy(b, b2, bytesRead); arrays.Add(b2); } else if (bytesRead &gt; 0) arrays.Add(b); } } foreach (var b in arrays) { BitArray source = new BitArray(b); 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]; var back2byte = BitArrayToByteArray(target); arrays.Clear(); arrays.Add(back2byte); } using (var f = new FileStream("file.cfg.enc", FileMode.Open)) { foreach (var b in arrays) f.Write(b, 0, b.Length); } </code></pre> <hr> <p><strong>EDIT 2:</strong> Here is the Ugly Betty-looking code that accomplishes what I wanted. Now I must refine it for performance...</p> <pre><code>var arrays_ = new List&lt;byte[]&gt;(); var arrays_save = new List&lt;byte[]&gt;(); var arrays = new List&lt;byte[]&gt;(); using (var f = new FileStream("file.cfg.dec", FileMode.Open)) { for (int i = 0; i &lt; f.Length; i += 4) { var b = new byte[4]; var bytesRead = f.Read(b, 0, b.Length); if (bytesRead &lt; 4) { var b2 = new byte[bytesRead]; Array.Copy(b, b2, bytesRead); arrays.Add(b2); } else if (bytesRead &gt; 0) arrays.Add(b); } } foreach (var b in arrays) { arrays_.Add(b); } foreach (var b in arrays_) { BitArray source = new BitArray(b); 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]; var back2byte = BitArrayToByteArray(target); arrays_save.Add(back2byte); } using (var f = new FileStream("file.cfg.enc", FileMode.Open)) { foreach (var b in arrays_save) f.Write(b, 0, b.Length); } </code></pre> <p><strong>EDIT 3:</strong> Loading a big file into byte arrays of 4 bytes wasn't the smartest idea... I have over 68 million arrays being processed and manipulated. I really wonder if its possible to load it into a single array and still have the bit manipulation work. :/</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.
    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