LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

String to Number Conversion Problem

I'm having trouble determining the problem with the "Fract/Exp String To Number" VI. The output isn't what I'm expecting (even when I try to do the conversion manually). I've attached a VI that will hopefully explain more than I can type here. The default values are the values I get when I run the VI. This is just an example number, but it seems to have problems with every number that has a decimal value. I appreciate any help I can get.

 

~jc

 

LabVIEW 8.2.1

0 Kudos
Message 1 of 14
(4,729 Views)

JRC_Tinker wrote:

 The output isn't what I'm expecting (even when I try to do the conversion manually)


Maybe you could tell us what you're expecting 😄

 

DBL datatype is only good for about 15 decimal digits, so anything longer will get truncated. There is no way around this unless you create your own datatype.

Message 2 of 14
(4,723 Views)
Message 3 of 14
(4,720 Views)

Thanks for the quick reply.

 

I'm really only expecting to get a DBL value that matches the Example string. I figured the size of the string might be the issue, but I also get strange outputs when I run the VI with an example string of '1.2' in the control (or several other smaller values). I was wondering if it was a 'digits of precision' problem, but I can't seem to figure out why the manual conversion doesn't work either.

 

~jc

0 Kudos
Message 4 of 14
(4,712 Views)

Nice round decimal fraction (e.g. 0.1) are often not representable with a finite number of binary bits (and vice-versa).

 

Set your display to show a reasonable number of decimal digits and you're ok. How much precision do you actually need?

Message Edited by altenbach on 04-21-2009 02:18 PM
Message 5 of 14
(4,703 Views)

Thanks again for the help. I was excited to see you reply because your answers always seem to make good sense.

 

As for the precision required...it is tough to answer because it's part of a larger problem that I'm trying to solve. Namely, is it possible to convert string data and store it in a DBL?...then convert it back later. I was hoping I would be able to store it as eight 8-bit ASCII characters (or at least 7 char). I can attach another VI showing my test process if it would help, but I was basically converting the string to a Boolean array and then converting the Boolean array to a DBL (with four characters on either side of the decimal). Is there a better way to do this? Storing in DBL format will require much less rework to a larger application. 

 

~jc

0 Kudos
Message 6 of 14
(4,674 Views)

JRC_Tinker wrote:

Namely, is it possible to convert string data and store it in a DBL?...then convert it back later. I was hoping I would be able to store it as eight 8-bit ASCII characters (or at least 7 char).


Well, a DBL is 64bits and we already determined that it is insufficient to hold your required data. How long are your strings? Is the decimal point always in the same place? Are you simply looking for some kind of compression?

 

Yes, please attach an example.

Message 7 of 14
(4,659 Views)

I apologize if the information is coming a little at a time. I will try to explain in a little more detail.

 

My application is currently designed (and implemented in industry) to acquire data from several sources and store it as arrays of DBLs in a datalog file. This data can then be used in the future to "playback" the events that are captured. A new requirement to read from a serial data source has been introduced and I can't be sure that the data from this source will only be numeric data. So I'm looking for a way to account for the storage of alphanumeric strings to save me from having to restructure the entire data storage structure.

 

I've attached the (attempted) conversion program that may shed some light on my efforts. As you can see the first part is converting correctly, but the decimal portion does not match so the conversion isn't correct. Again...I'm sure there is another (better) way to do this, but I haven't thought of it yet.

 

In summary and to answer your questions...the decimal point may not be in the same place since it may not exist at all (i.e. the case of an alphanumeric string). Also, I'm really only looking for a way to store the data, but if I could get some sort of compression to have more characters then that would be a bonus.

 

Thanks again for all your help.

~jc
0 Kudos
Message 8 of 14
(4,628 Views)

After playing around a little bit more I was able to get the conversion to work for a maximum of 6 ASCII characters. I have attached the updated file.

 

However, I would still be interested to know if there is any other way to store STRING information in a DBL format...especially if I can store more characters.

 

Thanks,

~jc
0 Kudos
Message 9 of 14
(4,615 Views)

Wow, that seems incredibly convoluted. I am sure all that could be done with code the size of a postage stamp. Also notice that you waste two extra bytes for the decimal point and size.

 

Without compression, you could just typecast the string to an array of DBL (possibly after appropriate padding). If the string can only contain a limited subset of characters, compression is possible. Maybe I have time to look into this later today.

 

Typecasting works if the string is exactly 8 bytes long. So if your string is 7 or less bits, you can encode the actual lenght in the first byte and then pad the string before casting and reverse the operation at the other end. The attached VI shows the simple typecasting version that assumes the string is 8 bytes. Modify as needed.

 

 

Message 10 of 14
(4,611 Views)