LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Initialize string of a known length

Solved!
Go to solution

I've looked for this in the forums, but didn't find anything. I'm trying to convert a string to its equivalent ASCII representation. So I use the String to Byte Array function. But then I want to have a string with all those bytes concatenated together. One way to do that is to initialize an empty string into the shift register of a For loop, and then use the Number to Hexadecimal String inside the For loop and concatenate the bytes. See the following snippet for the code:

 

String to ASCII Conversion.png

 

 

The problem, then, is that you have a Concatenate String function inside a For loop, which calls the Memory Manager each iteration of the For loop, unless I'm mistaken. Since I know, before going into the For loop, how long the resulting string is going to be (just twice the number of elements in the byte array), what I would prefer to do is initialize a string of the appropriate length, and then simply replace the string elements as I go. Something like the the following:

 

String to ASCII Conversion Improved Memory Management.png

 

How do I do what's represented by my comment? I couldn't care less what goes into the string, obviously. I just want the correct length string going into the For loop. Any ideas?

 

Thanks!

Adrian C. Keister, Ph.D.

Certified LabVIEW Architect
Certified Professional Instructor

B.S. in Applied Physics/Computer Hardware and Software Systems, Mathematics, 2001, Grove City College.
M.S. in Mathematical Physics, 2004, Virginia Polytechnic Institute and State University.
Ph.D. in Mathematical Physics, 2007, Virginia Polytechnic Institute and State University.
0 Kudos
Message 1 of 18
(5,617 Views)

If I'm not mistaken, you don't need a FOR Loop.  YOu can wire U8 array to the "number to hex string", which gives you a array of hex strings and then use "array to spreadsheet string to get the requested string.  (this is for your first snippet 😉

Kind regards,

- Bjorn -

Have fun using LabVIEW... and if you like my answer, please pay me back in Kudo's 😉
LabVIEW 5.1 - LabVIEW 2012
Message 2 of 18
(5,607 Views)

Initialize a Byte Array [U8] to the desired length and then convert it to a string.

 

Lynn

Message 3 of 18
(5,604 Views)
Solution
Accepted by topic author akeister

I'm not really sure what you're trying to do, but is this close?

initialized string.png

 

Message 4 of 18
(5,595 Views)

While you have been given answers to your specific question I thought I would answer your more general question. That is if you are concerned about LabVIEW's memory management performance (which is actually very good) and want to pre-allocate a string when you need to iterate over it while building it you can initialize a byte array to your desired size. Feed that to the loop and use replace array subset to replace the elements with the actual data. After the loop complets use the byte array to string to get the string itself. This will reduce the memory allocations required when the code is running.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 5 of 18
(5,583 Views)

 


majoris wrote:

initialized string.png


Whatever you do, don't forget to set the width=2 for "number to hexadecimal string", else all bets are off.

 

Message 6 of 18
(5,574 Views)

Reply to All,

 

One thing I forgot to mention: the resulting string is going to get written to an RFID tag that has a total of 62 bytes of non-volatile storage on it. Therefore, every single byte counts. In addition, the algorithm will be running on an sbRIO platform, and hence my concern for careful memory management.

 

ABCPrograms,

 

Thanks for your answer. A couple of thoughts on it:

 

1. There's a coercion dot on the "Number to Hexadecimal String" function. It expects I32's, it seems. I suppose this coercion dot is justified, since it'll only happen once, but it would still be nice if there wasn't one, since it'll be converting an entire array to I32's. 

 

2. The "Array to Spreadsheet String" function appears to insert tabs between all the bytes. Is there a way to prevent it from doing this? I tried wiring an empty string constant to the delimiter (Tab) unput, but that didn't work. I really don't want to remove the tabs after the fact, since that's yet more processing.

 

johnsold,

 

Thanks for your answer. Question, though: you say, "Convert it to a string." With which function? Remember that I don't want my original string back, but the ASCII hex codes. 

 

majoris,

 

Very nice! That does exactly what I want, and since it's not in a for loop, I'm thinking the Memory Manager will only have to be called once, say. 

 

Mark Yedinak,

 

That sounds like johnsold's solution. Is that correct?

 

altenbach,

 

It actually works just fine without wiring anything to the "width" input. 

 

So far, unless someone else has a better solution, it looks like majoris's solution is the best.

Adrian C. Keister, Ph.D.

Certified LabVIEW Architect
Certified Professional Instructor

B.S. in Applied Physics/Computer Hardware and Software Systems, Mathematics, 2001, Grove City College.
M.S. in Mathematical Physics, 2004, Virginia Polytechnic Institute and State University.
Ph.D. in Mathematical Physics, 2007, Virginia Polytechnic Institute and State University.
0 Kudos
Message 7 of 18
(5,567 Views)

Hi Akeister,

 

you don't need to use the array to spreadsheet string, you can use the easier solution (concatenate shown by majoris).  it will do the same without the overhead 😉

Kind regards,

- Bjorn -

Have fun using LabVIEW... and if you like my answer, please pay me back in Kudo's 😉
LabVIEW 5.1 - LabVIEW 2012
Message 8 of 18
(5,554 Views)

I agree with Mark that overall it might be better to work on an array of U8.

 

Here is an image of what I recommended initially.  Obviously initializing the array to some other value, like zero, would be more useful.  This is for illustration.

 

Lynn

 

Byte Array to String.png

Message 9 of 18
(5,545 Views)

Reply to johnsold,

 

Your code actually looks more like the inverse function (which I do need, actually, since I'm writing both the code that writes RFID tags, and the code that reads RFID tags): translates a hex representation of an ASCII code into human-readable characters. 

Adrian C. Keister, Ph.D.

Certified LabVIEW Architect
Certified Professional Instructor

B.S. in Applied Physics/Computer Hardware and Software Systems, Mathematics, 2001, Grove City College.
M.S. in Mathematical Physics, 2004, Virginia Polytechnic Institute and State University.
Ph.D. in Mathematical Physics, 2007, Virginia Polytechnic Institute and State University.
0 Kudos
Message 10 of 18
(5,540 Views)