03-21-2011 09:14 AM
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:
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:
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!
Solved! Go to Solution.
03-21-2011 09:23 AM - edited 03-21-2011 09:25 AM
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 😉
03-21-2011 09:24 AM
Initialize a Byte Array [U8] to the desired length and then convert it to a string.
Lynn
03-21-2011 09:29 AM
I'm not really sure what you're trying to do, but is this close?
03-21-2011 09:38 AM
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.
03-21-2011 09:44 AM
majoris wrote:
Whatever you do, don't forget to set the width=2 for "number to hexadecimal string", else all bets are off.
03-21-2011 09:48 AM
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.
03-21-2011 09:55 AM
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 😉
03-21-2011 10:07 AM
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
03-21-2011 10:14 AM
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.