LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how does type cast work for string to U16

Solved!
Go to solution

Hello,

 

Sorry for typing on same topic, but I still do not understant what will happen when I type cast 2-bytes string into U16. Could you please explain me this?

 

Regards,

__behemot_

0 Kudos
Message 1 of 8
(7,925 Views)

You will get the ASCII code of the first character as the high byte and the ASCII code of the second character as the low byte.

Or, in other words, the U16 value will be 256*asc(ch1)+asc(ch2). asc() is the ASCII code of a character.

 

0 Kudos
Message 2 of 8
(7,907 Views)
Solution
Accepted by __behemot_

Hi,

 

All of ASCII character are represented in 8 bits in the memory. If you imagine these 8 bits, you can concatenate them. That will be the 16 bit long integer (in this case, when you have a 2 elements string), and obviously unsigned, because there is no sign defined in ASCII values.

 

So, both of integer and character are represented in the same way in the memory, but LabVIEW knows that which type is stored in which variable. If you want to operating with ASCII characters as you would have numbers, you should cast it to integer then you can multiply it and etc. For instance, if you want to change 'a' to 'A', you can cast it to U8 integer than you should substract 20 from it and cast it back to character (1 element string).

 

I hope it will help you to understand type casting. If you have further questions, please write again.

 

Regards,

Peter

 

Peter Vago
Download All
0 Kudos
Message 3 of 8
(7,883 Views)

@PeterV_hun wrote:

... if you want to change 'a' to 'A', you can cast it to U8 integer than you should substract 20 from it and cast it back to character (1 element string).

 


That would be 20 in hex representation (or 32 in decimal).

0 Kudos
Message 4 of 8
(7,873 Views)

Right, thanks 🙂

 

Peter

Peter Vago
0 Kudos
Message 5 of 8
(7,871 Views)

Thank you very much for all responses. It was very usefull. 🙂

0 Kudos
Message 6 of 8
(7,858 Views)

Hi everyone.

 

I've a question about the given answer.

 

How to adapt it to convert string of 4 characters in a u16 word?

 

In fact,  i read from a csv file, value like 0x0001, 0xFFEB etc.  I know how to cut '0x' but no to cast from string to U16.

 

I need to do this, because, i need to use subvi who takes U16 value on inputs.

 

Thanks

 

 

0 Kudos
Message 7 of 8
(7,728 Views)

No Type Cast required here (darn), simply use Hexadecimal String to Number (clip the 0x), or skip the middle man and use Spreadsheet String To Array to go directly from CSV data to the U16 Array.

 

HexStringExample.png

0 Kudos
Message 8 of 8
(7,717 Views)