LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
TobiasT

Get number of bits of data types

Status: Declined
Customer found an acceptable implementation using existing features.

Hi,

 

I am looking for a function that gives the number of bits (or bytes) that a given data type uses on disk and could not find it so far. LV types use constant number of bits for each specific data type, e.g. a string is always 8 bit, a single numeric is 32 bit and a double is 64 bit. It would be helpful to have a function that takes any data type as input and gives back the number of bits used to save the data on disk.

 

With this function one could write VIs that read/write binary files generic. Without this function I have one VI that reads double numerics from a binary file (e.g. the 4th number is on positioin 4*8) and a different VI to read an U8 numeric (the 4th number is on poistion 4*1). The only difference between those two VIs is the constant for a doulbe (8 bytes) and a U( (1 byte).

 

So basically I would prefer the "GetNumberofBytesOfDataType.vi" shown at the bottom of the attached example implemented as a LabVIEW native function.

 

Regards,

  TT

8 Comments
TobiasT
Member

Attachment did not work, copied below is a screenshot.

 

Regards,

  TT

 

ReadFromBinaryFile_Example.png

fabric
Active Participant

"Flatten To String" + "String Length" should get you there:

GetSize.png

 

In my example we have 4 bytes for the SGL, 1 byte for the boolean, and 7 bytes for the string (3 chars + 4 bytes for string length).

 

TobiasT
Member

Thanks very much, I updated my example as given below. And it works for numbers and bools, for strings I get 4 bytes more (due to the string length) as you pointed out.

 

With this I can change the VAR constant to a VAR Control and then use this VI as a subVI for any data that I want to write to a binary file. I then don't have to care about the byte number in the calling VI.

 

I hoped to find this function within LV, but since the workaround with the "Flatten to String" and "String LEngth" functions is so easy I am happy with it.

 

ReadFromBinaryFile_wFlattenToString.png

crossrulz
Knight of NI

I think you are misunderstanding what the Count means for the Read Binary file.  The count in the number of the wired data type to read.  So having a double wired to the type will read Count doubles.  So you are actually trying to read 8x more data than you need.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
TobiasT
Member

Of course you are right.

I performed the LV Core Lecture "Using binary files" yesterday and somehow misunderstood an important point - they read the file size in bytes of a binary file and divide it by the number of bytes per element to get the number of elements in the file. This number of elements is then wired to the Read from binary file function. I somehow thought it has to be done vice versa and hence came to the idea above.

So after playing around with it I see that this is complete non-sense and in that part LV is as intuitive as it should be 😉

 

Thanks for your help crossrulz and fabric.

  TT

altenbach
Knight of NI

I am not sure why this is posted in the ideas exchange. Seems more like a regular forum discussion.

TobiasT
Member

Well, it was an idea in the first post (to have a LV function that gives back the number of bytes that a data type uses on disk), but then I learned that the reason I wanted to use it for is non-sense (however there might be other uses for that function). So I agree that I had better posted it in a forum discussion, but I can't change it to the forum now.

 

JordanG
NI Employee (retired)
Status changed to: Declined
Customer found an acceptable implementation using existing features.