09-10-2012 04:11 PM - edited 09-10-2012 04:19 PM
Does anyone know if there's a byte to string function? I have a single byte I need to convert from a U8 byte array to an acsii string char. Does this exist?
09-10-2012 04:20 PM - edited 09-10-2012 04:21 PM
@QRP wrote:
Does anyone know if there's a byte to string function? I have a single byte I need to convert from a U8 byte array to an acsii string char. Does this exist?
You are kind of mixing and matching what you say vs. what you want. You have a couple options. If you have a byte array you want to make a string, there is a byte array to string function. If you want the ascii character associated with a single u8 value, you can also use a type cast. It all depends what you are trying to get, but I think what you are getting at is that you need the byte array to string, which yes, there is a funciton for that.
09-10-2012 04:21 PM
There is a "Byte Array To String" function.
If you take your single byte and 'build' it into an array of length=1, then you can convert it to an ASCII character.
09-10-2012 04:23 PM - edited 09-10-2012 04:24 PM
@LandBelenky wrote:
There is a "Byte Array To String" function.
If you take your single byte and 'build' it into an array of length=1, then you can convert it to an ASCII character.
In this case, type cast is a better option so there is no array building involved. It just takes the bit pattern in memory and interprets it as a string instead of a numeric.
09-10-2012 04:32 PM - edited 09-10-2012 04:32 PM
The typecast function is really all you need. Leave the central connector unwired and it will convert your U8 array of your U8 scalar to a string.
09-10-2012 04:37 PM
Maybe you know, Altenbach, but does the byte array to string just do a type cast in the background? If so, is there any additional purpose to this function considering you can get the same functionality just as easily with a type cast?
09-10-2012 04:38 PM - edited 09-10-2012 04:39 PM
It's worth noting that Typecast is sensitive to the data type fed to it.
If you send it "65" as a byte, you get "A." But if you send it "65" as a single, you get "B,". (or B\82\00\00, in slash code.)
Other data types yield different results.
If you use the Byte Array to String, you can use any data type and still get the same answer.
09-10-2012 06:40 PM
@LandBelenky wrote:
It's worth noting that Typecast is sensitive to the data type fed to it.
I did not suggest to use other datatypes except U8. Of course typecast will fully adapt to the unput datatype, and the's the beauty of it. 😄
@LandBelenky wrote:
If you use the Byte Array to String, you can use any data type and still get the same answer.
Only if the values in "that other data type" fit within the range of U8. In all other cases you'll probably get garbage too. Just because "byte array to string" coerces the input to U8 does not make it any safer to the programmer who is ingnorant ofdoes not watch out for representations.
@for(imstuck) wrote:
Maybe you know, Altenbach, but does the byte array to string just do a type cast in the background? If so, is there any additional purpose to this function considering you can get the same functionality just as easily with a type cast?
I assume that the compiler generates the same code for both functions, but I don't know for sure. I guess "byte array to string" and "string to byte array" are common enough to deserve their own function in the palette, just to make it easier for the new programmer. (Using typecast for "String to byte array" requires a type diagram constant, so is a bit more bulky.)
Having both "byte array to string" and "string to byte array" keeps the universe in balance.
Note that LabVIEW does not always have nicely matched forward/inverse pairs of functions. Some examples:
09-10-2012 08:21 PM
@for(imstuck) wrote:
Maybe you know, Altenbach, but does the byte array to string just do a type cast in the background? If so, is there any additional purpose to this function considering you can get the same functionality just as easily with a type cast?
These functions are NOT identical! There's an important difference: Byte Array to String operates in-place whereas Type Cast always creates a copy.