02-22-2011 04:56 PM
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
02-22-2011 05:24 PM - edited 02-22-2011 05:26 PM
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?
02-22-2011 05:28 PM
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
02-23-2011 01:46 AM
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
03-30-2016 01:11 PM
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?
03-30-2016 01:25 PM
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).
03-30-2016 01:28 PM - edited 03-30-2016 01:30 PM
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.
03-30-2016 01:51 PM
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.
03-30-2016 01:57 PM - edited 03-30-2016 01:59 PM
@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.
03-30-2016 02:02 PM - edited 03-30-2016 02:10 PM
@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.