chrisb4748

String to Decimal

3 posts in this topic

Hi,

I am looking to convert string into decimal. I'm using GXWorks3.

Is there a function block to do this? 

When I look in the manual I can only find decimal to string.

Many Thanks

Edited by chrisb4748

Share this post


Link to post
Share on other sites

If you interpret the ASCII table in decimal, the number characters start at 48 for zero. Therefore if you subtract 48 from any number character byte, you will get its unit value. To get the tens, multiply by 10. To get the 100s multiply by 100 etc. Then sum them to get the number.

E.g. (57-48*100)+(52-48*10)+(53-48*1) = 945

Because Mitsubishi PLCs have maths functions which take data 2 bytes at a time, to get the single byte you could mask the byte you don't need with WAND. Then shift the byte to the right place with SHR.

E.g if you have '34' in D1, the following should give you decimal 3 in D10.

WAND HFF00 D1 D10

SFR D10 k8

- D10 k48 D10

Edited by Luke.S

Share this post


Link to post
Share on other sites

I'm not sure if you're asking for conversion into decimal, or integer? You have some instructions for converting from string to "many" other formats and they all start with "STRING_TO_***". So if you want to convert into integer: STRING_TO_INT, if you want to convert into decimal/real: STRING_TO_REAL.

BUT: Remember that a 'null' or non-value string will cause an error in the PLC (invalid operation). So make sure you have a valid string input. This means that the first thing you must check is that there's actually a value in the string (if there is no value at all, then just move '0' into the string).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now