# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## U16 TO STRING AND STRING TO U16 CONVERSION

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.

Message 1 of 9
(2,153 Views)

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

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)

Message 2 of 9
(2,123 Views)
Solution
Accepted by vihang

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

@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".

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
(2,122 Views)

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

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.

Message 4 of 9
(2,121 Views)

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

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:

Message 5 of 9
(2,114 Views)
Solution
Accepted by vihang

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

This is all you need:

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

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

Thanks

This worked for me

Message 7 of 9
(2,047 Views)

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

Thanks

This is superb!!!

Message 8 of 9
(2,043 Views)

## Re: U16 TO STRING AND STRING TO U16 CONVERSION

@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):

Message 9 of 9
(2,007 Views)