From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a string to an array of character

What would be the easiest way to convert a string "HELLO" to an array of character "H" "E" "L" "L" "O" ?
I have a way to do it with a for loop but I'm wondering if there is nothing even more simple.

Thanks in advance,
Martin
0 Kudos
Message 1 of 23
(3,945 Views)
I think I found something easy. Still with a for loop but very small. See the attached file.

Martin
0 Kudos
Message 2 of 23
(3,941 Views)
I can't really think of a better way to do it than this:

Hope this helps

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 3 of 23
(3,940 Views)
Try typecast instead. 😉
0 Kudos
Message 4 of 23
(3,942 Views)
I just saw your answer after I posted mine.

I tried out your version and it gives the correct results, but mine is approximately 30% faster. I don't actually know why. Maybe someone could enlighten us?

Hope this helps

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 5 of 23
(3,932 Views)
Thank you altenbach,

I should definitely read more about typecast to use it more often.

Martin
0 Kudos
Message 6 of 23
(3,929 Views)
Hi Altenbach,

just saw your answer too. Using the typecast is slower than the original version with making an array and setting the array to a string. The fact that it is slower simply doesn't compute with me, but I've just measured it(P4 2.8 GHz, hyperthreading).

Hmmm.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 7 of 23
(3,929 Views)
Just to show I've been doing something, here are the times required to modify the entire text of "Macbeth" from a string to a string array.

My version : 27.98 msec
Martin's version : 46.88 msec
Altenbach : 71.77 msec

Can someone from NI explain why this is, as I personally thought Altenbach's version would be much faster when I saw it.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 8 of 23
(3,923 Views)
For a short string of 5 characters, I get the following times on my rig (1GHz PIII):

Martin 2.5 microseconds
Me 4.3 microseconds
Shane 0.8 microseconds

For a 1MB string, I get the following:

Martin 2.2 seconds
Me 2.6 seconds
Shane 1.9 seconds

So, yes, mine's slowest. 😞 Martin, use whatever is best.
0 Kudos
Message 9 of 23
(3,908 Views)
Altenbach,

Your code is still the nicest though ;).

I'm still left asking the question why the results are in this order. From my (obviously incorrect) knowledge of HOW this SHOULD work, I thought you were on a sure-fire winner.

Does anyone know why this isn't the case?

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 10 of 23
(3,901 Views)