LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get only the string out of a U8 array?

Solved!
Go to solution

Good day!

 

In my logic, which is different to what LabVIEW does, a string doesn't contain any 0x00 bytes or is delimited by one. LabVIEW strings can also contain more than one 0x00 making its length longer than the number of actual ASCII characters. That is, unfortunately, what the standard VI "Byte array to string" does. It keeps trailing zeros.

The other standard VI "Trim whitespaces" would also not remove the zeros, though they are not ASCII characters.

 

How to get rid of the trailing zeros? Is there no VI for it? Of course I could program something to find the first 0 and split the string, but that's it?

Can someone say, why "Byte array to string" wouldn't remove the trailing zeros?

0 Kudos
Message 1 of 23
(3,335 Views)
Solution
Accepted by topic author MaSta

Hi MaSta,

 


@MaSta wrote:

LabVIEW strings can also contain more than one 0x00 making its length longer than the number of actual ASCII characters. That is, unfortunately, what the standard VI "Byte array to string" does. It keeps trailing zeros.

The other standard VI "Trim whitespaces" would also not remove the zeros, though they are not ASCII characters.


Why do you think NUL is not a valid ASCII char?

And yes, LabVIEW strings can contain any byte values from 0x00 to 0xFF…

 


@MaSta wrote:

Can someone say, why "Byte array to string" wouldn't remove the trailing zeros?


As said before: LabVIEW strings can contain ANY byte values. They keep their length information seperately!

 


@MaSta wrote:

How to get rid of the trailing zeros? Is there no VI for it? Of course I could program something to find the first 0 and split the string, but that's it?


When you want to remove "trailing zeros" then you should NOT search for the first 0x00 byte…

 

  • You can look inside the TrimWhitespace function and replicate it's behaviour (for trailing bytes) in you own function.
  • You can also work on the byte array level using array functions and loops (autoindexing, conditional tunnels).
  • You can prevent those trailing zeros when creating those byte arrays/strings to avoid cutting strings later in the process…

This is what TrimWhitespace is doing:

You can easily copy that code and change it to remove 0x00 bytes at the end…

 


@MaSta wrote:

In my logic, which is different to what LabVIEW does


That is the main problem in this question… 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 23
(3,319 Views)

hello MaSta,

 I am Assuming that you need to convert the byte array to Hexadecimal String, Byte Array to String does not add trailing zeros and it is difficult to understand your logic without seeing what you have implemented.

Share the logic you  have implemented and what is the output you need to achieve ?

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
Message 3 of 23
(3,317 Views)

No, a normal string, but without the zeros, as LabVIEW would have it when you create a string constant. 

My solution now is 

MaSta_0-1692010141610.png

 

@GerdW: I hear you, but that doesn't make it logical. "In computer programming, a string is traditionally a sequence of characters". Byte 0x00 isn't a character, it's a value, a number. I actually expected LabVIEW to handle it just like that. Well, you never stop learning...

0 Kudos
Message 4 of 23
(3,281 Views)

Hi MaSta,

 


@MaSta wrote:

@GerdW: I hear you, but that doesn't make it logical. "In computer programming, a string is traditionally a sequence of characters". Byte 0x00 isn't a character, it's a value, a number. I actually expected LabVIEW to handle it just like that. Well, you never stop learning...


You never stop learning!

From your link to "characters" you can read this paragraph:

Examples of characters include letters, numerical digits, common punctuation marks (such as "." or "-"), and whitespace. The concept also includes control characters, which do not correspond to visible symbols but rather to instructions to format or process the text. Examples of control characters include carriage return and tab as well as other instructions to printers or other devices that display or otherwise process text.

 

So I repeat: 0x00 is a valid ASCII char (named NUL)! I already linked to the ASCII table in Wikipedia in my previous message.

It's the very same character class as is BEL, LF, CR, ESC, FF, STX, ETX, …

 

What you are referring to are null-terminated strings, which are used only by a subset of programming languages (due to several limitations described in the linked article).

As I wrote before: LabVIEW stores the string length separated from the string data!

 

For me this all sounds very logical, so I still guess the problem is with your logic (aka programming experience with other programming languages)… 😉

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 23
(3,276 Views)

@MaSta wrote:

@GerdW: I hear you, but that doesn't make it logical. "In computer programming, a string is traditionally a sequence of characters". Byte 0x00 isn't a character, it's a value, a number. I actually expected LabVIEW to handle it just like that. Well, you never stop learning...


What you are expecting is a NULL-terminated string, what I usually hear call a C-style String.  What LabVIEW implements is a Length-Prefixed string, which has the length of the string (I32 in this case) before the actual bytes/characters in the string.  When using this method, a NULL is a valid character.


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 23
(3,257 Views)

@GerdW wrote:

What you are referring to are null-terminated strings, which are used only by a subset of programming languages (due to several limitations described in the linked article).

As I wrote before: LabVIEW stores the string length separated from the string data!

Like Pascal, IIRC.

 

C++ is often used as the norm, but it really isn't and certainly hasn't always been.

 

There are ups and downs with "Pascal strings". The only downs are when dealing with external code, like most Windows APIs. LV never cared much for interfacing with external code.

 

Message 7 of 23
(3,252 Views)

@MaSta wrote:

No, a normal string, but without the zeros, as LabVIEW would have it when you create a string constant. 

My solution now is 

MaSta_0-1692010141610.png


If you want to remove all \00's, you might as well use Search and Replace String:

wiebeCARYA_0-1692013657307.png

If you only want the beginning of the string, you can use split string:

wiebeCARYA_1-1692013888185.png

 

Note those display styles. They're useful...

 

If you want to use the match pattern, use \00, not \0 (although it works). If you add a letter to it, \0 won't work:  \0a vs \00a. It's officially 2 digits.

 

Further note that with regular expressions (not match pattern) (also used in Search And Replace String when turned on) you should use \000, at it's a 3 digit octal number, or \x00 to specify a hex number.

Message 8 of 23
(3,237 Views)

As has been said, you seem to have problems with terminology:

 


@MaSta wrote:

How to get rid of the trailing zeros?


A "zero" is ASCII character 48 (decimal), which is completely different to your \00 (0 in decimal), i.e. ASCII NUL,

Any byte value from 0..127 is defined in the ASCII table, and the rest, up to 255, as extended ASCII. ASCII is just an ancient convention that assigns special meaning to byte values (control character, printable character, etc), for example a printer that receives a decimal byte of value 65 will print the letter "A". while a decimal value of 10 would make it go to a new line.

 

Are the only \00 in your string "trailing" or could the be others? Do you know why they are there in the first place? How many are there? Where does your string come from? (Instrument? Some third party driver? Some DLL? Your LabVIEW code? etc.)

0 Kudos
Message 9 of 23
(3,188 Views)

wiebe@CARYA wrote:

There are ups and downs with "Pascal strings". The only downs are when dealing with external code, like most Windows APIs. LV never cared much for interfacing with external code.


Never cared? It has a Call Library Node with a few 100 options which is generally a few 100 options to much for most users! Do those few 100 options cover every possible craziness out there in external code? No of course not, that would require a few 1000 options and that would be a few 1000 options to much for pretty much every user.

Rolf Kalbermatter
My Blog
Message 10 of 23
(3,180 Views)