LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read all bytes from any file

Solved!
Go to solution

@Pepe727 wrote:

Hey! I would like to read any type of file and read them as binary (Obtain an 1d array of U8 or an 2d array 8bits x sizearray) and when I have manipulated it, rebuild the original file.


A file is just a generic linear stream of bytes. What you do with it is up to you. You can read it raw, process it, and write it back raw using the tools already mentioned by others.

There is only one dimension: Each element has exactly one index (its position in the file!) so your question about a 2D array requires clarification. Of course you can think of the bits in a byte as a second dimension, but that does not make it a 2D array unless you inflate it to some much more inefficient 2D boolean array for example where each elements corresponds to a bit. This will take 8x more memory and will slow you down and is not needed.

 

All boolean operations will work on integers directly and will do bitwise operations if applied. That's all you need for e.g. encryption or other processing. The code should be mostly blue. No need for green datatypes.

 

Here's one possibility:

 

 ProcessFile.png

 

To ensure correct operations, you need to be careful.

 

  • Make sure the file positions is at the beginning of the file. (true if you just opened it, else you can set the position)
  • Read the entire file as U8 array (either 1 U8 array or -1 U8 elements). No need for strings!
  • When writing back, don't prepend the size (this will add another 4 bytes to the start of the file)
  • While you might think that you could equally well read the file using the text file functions, you need to be careful to disable e.g. eol conversion, etc. so don't do that.
  • Arrays in LabVIEW are limited in size to nonnegative I32 indices (0 .. 2147483647), but a file can be much larger. If the file is larger, you need to process it in chunks or find other workarounds.

 

0 Kudos
Message 11 of 11
(214 Views)