Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a byte array

Tags:

arrays

c#

split

I have a byte array in memory, read from a file. I would like to split the byte array at a certain point (index) without having to just create a new byte array and copy each byte at a time, increasing the in memory foot print of the operation. What I would like is something like this:

byte[] largeBytes = [1,2,3,4,5,6,7,8,9];   byte[] smallPortion;   smallPortion = split(largeBytes, 3);   

smallPortion would equal 1,2,3,4
largeBytes would equal 5,6,7,8,9

like image 396
Keith Sirmons Avatar asked Aug 21 '08 19:08

Keith Sirmons


People also ask

How do you split bytes in Python?

Solution: To split a byte string into a list of lines—each line being a byte string itself—use the Bytes. split(delimiter) method and use the Bytes newline character b'\n' as a delimiter.

How do you add two byte arrays?

To concatenate multiple byte arrays, you can use the Bytes. concat() method, which can take any number of arrays.

What is a byte array?

ByteArray is an extremely powerful Class that can be used for many things related to data manipulation, including (but not limited to) saving game data online, encrypting data, compressing data, and converting a BitmapData object to a PNG or JPG file.

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.


2 Answers

In C# with Linq you can do this:

smallPortion = largeBytes.Take(4).ToArray(); largeBytes = largeBytes.Skip(4).Take(5).ToArray(); 

;)

like image 138
Alireza Naghizadeh Avatar answered Oct 05 '22 07:10

Alireza Naghizadeh


FYI. System.ArraySegment<T> structure basically is the same thing as ArrayView<T> in the code above. You can use this out-of-the-box structure in the same way, if you'd like.

like image 37
Eren Ersönmez Avatar answered Oct 05 '22 06:10

Eren Ersönmez