From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading binary file bit-wise

Solved!
Go to solution

Hello, all. Thank you for your time. I am a new user, on the 2012 Windows version.

 

The short, broad question: is there any way to work with a binary file bit-wise, instead of byte-wise? Or, alternately, to use a custom data type with "read from binary file," which is not a full byte long?

 

The explanation:

I have data from, in essence, a counter. It dumps binary files in which each event has information encoded in a word of N bytes. However, subsets of information are not necessarily a full byte, nor are they "aligned" to bytes. (For the record, I can't change the data type)

 

A concrete example, slightly simplified but effectively identical: A word length is 2 bytes. In this word, the first 5 bits are an ID number, and the remaining 11 bits are a timestamp.

01101  11111000000

ID         Timestamp

 

I am trying to quickly parse this file, creating an array with the information.

 

I can do it in the following manner:

-Read data in an intermediate format (i.e. U16)

-Convert that format to binary

-Chop the binary string to the desired lengths

-Convert back to integer

However, this is prohibitively slow (for essentially real-time processing), because of the necessity to loop through a large array. There are several potential solutions, but I haven't been able to implement any. Suggestions on their implementation? Or other solutions?

 

-Create custom data type for "read from binary file." Hasn't worked thus far because even a sub-byte read will result in a byte change in file position, or else the necessary conversions take as long as the original solution.

-Convert entire file into a 2D binary array, split array, convert array to numbers. Can't get booleans to read from bits instead of bytes.

 

Your help is much appreciated!

0 Kudos
Message 1 of 7
(3,183 Views)
Solution
Accepted by topic author SkyeC

You don't show what you did with the first solution, but most likely you used numeric to bit array and all such shenigan. Instead use boolean logic and Rotate!

If that is still to slow for you there is nothing that will be faster except going to FPGA!

 

Boolean Logic.png

 

This is simply to give you an idea. You didn't specify the actual format on disk (Big Endian or Little Endian, Upper 5 bits or lower 5 bits for the ID, etc).

Rolf Kalbermatter
My Blog
Message 2 of 7
(3,162 Views)

Thank you! I will give that a shot.

 

The data is little endian, which is part of the problem but not insurmountable.

0 Kudos
Message 3 of 7
(3,155 Views)

Then use the Read Binary File node with the byte order input set to little-endian.

 

Boolean Logic 2.png

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 7
(3,141 Views)

@SkyeC wrote:

 

 

The short, broad question: is there any way to work with a binary file bit-wise, instead of byte-wise? Or, alternately, to use a custom data type with "read from binary file," which is not a full byte long?

 


To answer the original question.  No. There is no way to read a file bit-wise.  File operations are always based on bytes.  That is a basic fact of the operating system no matter what programming language you use.

Message 5 of 7
(3,099 Views)

This is looking like it might be the solution! Thank you. I will confirm as solution when I am sure that it is working. What is represented by the little "b" in the corner of your constant / how do I create it? I assume it's something to do with designating a binary representation? My code seems to be working without it.

 

I would never have suspected that boolean "and" worked in this manner. Thank you!

0 Kudos
Message 6 of 7
(2,913 Views)
Solution
Accepted by topic author SkyeC

@SkyeC wrote:

What is represented by the little "b" in the corner of your constant / how do I create it?


Right-click...visible items...radix.

 

On a side note, you don't even need a FOR loop (but it is slightly less memory efficient. If you want, insert a toU8 into the upper branch):

 

altenbach_0-1580413889543.png

Message 7 of 7
(2,888 Views)