LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

I need To Understand the "Type Cast Function"

Dears,

 

I need To Understand How the "Type Cast Function" Work, and if you can Give me an Example it will be Apperciated

 

 

BR

Ahmed

0 Kudos
Message 1 of 26
(20,273 Views)

In its simplest explanation Type Cast allows you to reinterpret a series of bytes. The numeric conversion functions convert from one data type to another and essentially do the same thing that Type Cast does, albeit with just numbers. Type Cast allows you to extend this to convert one data type to another, beyond just numbers. Thus, you could convert an array of 2 U8 values into a single U16 number by using Type Cast like this:

 

or you could reinterpret an array of lots of U8 values into an array of U16 values like this:

 

 

How you use it is entirely up to you, but it is an extremely powerful function. What are you trying to do?

Download All
Message 2 of 26
(20,260 Views)

LabVIEW is an early binding language, meaning that all data-types are always of a known datatype (string, path, cluster, U8).

 

In memory the data does not store the actual datatype ( you can revise the 'How LabVIEW stores data in memory for details) but only the actual data. Some of these datatypes are alike, an array of U8s for instance is the same as a String and you can use typecast for altering only the datatype.

 

Another example I know are old(er) HP/Agilent network analyzers, those analyzers had several ways of outputting date, either as an ASCII formatted string, or as a Floating point string. The string would contain the binary floating point of double array, fitting a IEEE754 representation (LabVIEW uses the same representation), you could get the string, and do a typecast to an array of doubles.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 3 of 26
(20,257 Views)

If you know some C a type cast is 'x = *(type *) &x' it doesn't change any data, just the interpretation.

Just like: Is 50 degrees hot or cold? It depends on if you measure in C, F or K ...

/Y

G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Certified-LabVIEW-Developer
Message 4 of 26
(20,226 Views)

I'm still not getting exactly what Typecast does...........it seems to convert short strings into concatenated ones? I'm searching the forums for a way of converting my incoming data (which is in hexadecimal format) into text.

 

For instance I have 4 sensors on a pcb. The data is received by Visa serial as 41 31 20 33 31 37 30. This is just one line of data coming in. I want to convert that to text which is A1 3170. I then want to take the value of 3170 only (read from the A1 sensor)  and store it in an array with all the other A1 values. I then want to output all the sensor values in a waveform. 

 

I'm still at the stage of trying to figure out how to do the hex to text conversion and not sure if Typecast will work here? 

0 Kudos
Message 5 of 26
(15,512 Views)

suzanneocallaghan wrote:  The data is received by Visa serial as 41 31 20 33 31 37 30.

To to make sure we are clear, it that data what is shown when you have the indicator in Hex Display or in Normal Display?  If in hex display, then you just need to change how the indicator is viewed (ie change it to normal view).


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
0 Kudos
Message 6 of 26
(15,504 Views)

All data is stored in memory as bytes.  What those bytes mean depends on the "data type".

 

If it is an integer U8/I8 a single byte means a value from 0 to 255 or (-128 to 127).  If it is a string, then a single byte represents a string character that comes from the ASCII table.

 

Typecast takes incoming set of bytes that has one data type, and has them interpreted as another data type.  (i.e. the type is cast to another type.)

 

So a byte that has a value of 41 could mean the number 41 if it is cast as a U8.  Or it could become a string consisting of the character "A" if it is typecast to a string.

 

Once your 7 bytes get cast to a string, you may need to use other string functions to strip out parts of it, or even use string to number to convert the characters of "3" "1" "7" "0" to the number 3170.

 

In message 2 above, the typecast converted an array of U8's into an array of U16's.  It took each two bytes and interpreted them as a single U16 integer.

Message 7 of 26
(15,498 Views)

If you want to understand Type Cast then look at Jean Pierre's "devilishly simple" VI in this post http://forums.ni.com/t5/BreakPoint/A-call-for-obfuscated-LabVIEW-code/m-p/198230#M45 Once you understand how it works you will understand Type Cast. The explanation comes later in that thread.

=====================
LabVIEW 2012


Message 8 of 26
(15,479 Views)

@crossrulz

 

Attached is a pic of my received data in front panel. For each line of data, i must ignore the first 11 characters and only deal with the last 7. The first 3 characters of that 7 are the sensor name. The last 4 characters are the sensor values. I have 4 sensors so I need to store the relevant values into 4 different arrays. 

 

@ravensfan

 

so typecast could convert my hex data to a string of numbers and characters

I need to use another function (?) to strip out the first 3 characters

And then use string-to-number function to convert the last 4 characters to a number

 

I dont suppose you know if the strip-out function exists?

 

thanks for the help so far.

 

 

0 Kudos
Message 9 of 26
(15,472 Views)

@suzanneocallaghan wrote:

I'm still not getting exactly what Typecast does...........it seems to convert short strings into concatenated ones? I'm searching the forums for a way of converting my incoming data (which is in hexadecimal format) into text.

 

For instance I have 4 sensors on a pcb. The data is received by Visa serial as 41 31 20 33 31 37 30. This is just one line of data coming in. I want to convert that to text which is A1 3170. I then want to take the value of 3170 only (read from the A1 sensor)  and store it in an array with all the other A1 values. I then want to output all the sensor values in a waveform. 

 

I'm still at the stage of trying to figure out how to do the hex to text conversion and not sure if Typecast will work here? 


EDIT Not keeping up with new information.  That looks like a "Chatty Cathy" UART with an insane FW developer on the other end.  What is the device we'll need to go to the manual for this.


"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 26
(15,466 Views)