Sign in to follow this  
Followers 0
PaulKraemer

Int to String (DTOS) - add leading zeros?

4 posts in this topic

Hi, I have a ControlLogix PLC. I was going to use the DTOS command to convert an integer to a string of ASCII characters. The thing that I am finding tricky is that for smaller numbers, I want to add leading zeros so that my string would always have four characters. In other words, I want: 1 = "0001" 10 = "0010" 100 = "0100" ...etc... Does anyone know of a way I can do this? Thanks in advance, Paul

Share this post


Link to post
Share on other sites
My first thought is to do the conversion first in a scratch location. Check its length, and concatenate it with the number of padding zeroes you need. I gotta run so no time to expand on the details... Paul

Share this post


Link to post
Share on other sites
Similar to OkiePC's method. Do the DTOS. have a fixed string of zeroes. Concatenate the two with your converted numeric second. From the result extract the required number of digits. Eg = Have a string of "000", from the number 29 you get "29". Concatenate that with the zeroes you get "00029". Now extract the rightmost 4 to get "0029".

Share this post


Link to post
Share on other sites
Simple program attached

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
Sign in to follow this  
Followers 0