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.
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.
01-29-2020 05:40 PM
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!
Solved! Go to Solution.
01-29-2020 05:55 PM - edited 01-29-2020 05:55 PM
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!
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).
01-29-2020 05:59 PM
Thank you! I will give that a shot.
The data is little endian, which is part of the problem but not insurmountable.
01-29-2020 06:15 PM - edited 01-29-2020 06:16 PM
Then use the Read Binary File node with the byte order input set to little-endian.
01-29-2020 07:27 PM
@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.
01-30-2020 12:52 PM
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!
01-30-2020 01:51 PM - edited 01-30-2020 01:52 PM
@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):