Wolfram Language

Read a File Directly as a ByteArray

The new function ReadByteArray can read in a file or stream directly to a ByteArray, bypassing the overhead of reading in a list of bytes and then creating the array.

Read in a file as a ByteArray.

Convert the first six bytes to a string in Latin-1 encoding. These identify the data as a GIF image.

The next four bytes encode the dimensions of the image as two 16-bit integers.

Confirm the dimensions by importing the binary data as an image.

Encode the data as Base64.

Base64-encoded data is one-third larger than the original data, but only uses letters, digits, + and /. This makes it safe to view or transmit via methods that might be confused by control or other special characters.

Reading in directly to a ByteArray instead of first reading in a list of bytes and then creating an array saves close to a factor of 8 in memory.

Related Examples

de es fr ja ko pt-br zh