LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

U16 TO STRING AND STRING TO U16 CONVERSION

Solved!
Go to solution

Hello

I am able to convert a U16 array to String successfully, but unable to get the other way round. i.e. String to U16 conversion

 

The attached code works good for string length if its even number, but for odd length the U16 array created is incomplete

 

Kindly guide.

0 Kudos
Message 1 of 9
(1,778 Views)

Your array input translates to  (in \code to show unprintable characters):

 

"HELLO\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"

 

Your string input is HELLO (i.e. 5 characters.) Obviously not the same!

(If you have an odd number of characters, you also need to decide to pad or truncate)

0 Kudos
Message 2 of 9
(1,748 Views)
Solution
Accepted by topic author vihang

@vihang wrote:

The attached code works good for string length if its even number, but for odd length the U16 array created is incomplete


So you need to detect if there are an odd number of characters in the string.  If there is, append a NULL character (0x00).

 

I would also recommend using the Flatten To String and Unflatten From String to convert the data.  There are inputs for the Endianess that you can set to "little-endian" and therefore avoid the need for "swap bytes".


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
Message 3 of 9
(1,747 Views)

If you are using a U16 for a string, each array entry is going to represent 2 characters. Since "HELLO" is an odd number of characters it seems like the swap bytes doesn't know what to do with only half of a U16 and doesn't return any data (which seems a bit odd to me). Any even length string seems to work in your example.

 

Anyway you need a way to make sure you have enough data to create a U16. The attached works, but I'm sure there are other ways that may be better to do this.

 

string_to_u16.png

Download All
0 Kudos
Message 4 of 9
(1,746 Views)

I would recommend "(un)flatten from/to string". It can include the endian conversion and even gives you a rudimentary error if things go wrong:

 

altenbach_0-1649266460471.png

 

0 Kudos
Message 5 of 9
(1,739 Views)
Solution
Accepted by topic author vihang

This is all you need:

sc.png

"If you weren't supposed to push it, it wouldn't be a button."
Message 6 of 9
(1,698 Views)

Thanks 

This worked for me

a.png

0 Kudos
Message 7 of 9
(1,672 Views)

Thanks

 

This is superb!!!

0 Kudos
Message 8 of 9
(1,668 Views)

@vihang wrote:

Thanks 

This worked for me

 


To pad to the next higher even size, you don't need any case structures.

 

Here's what I would do (note that bitwise operations are cheaper that Q&R, for example):

 

altenbach_1-1649345617816.png

 

0 Kudos
Message 9 of 9
(1,632 Views)