LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Format strings, need leading zeros

I have a data input where serial data is converted to a numerical value, then multiplied by a conversion factor, and the result then converted back into a string for outputting to a data file.

I have not been able to get small numbers, such as "497" to get converted into a string "0497". Have tried the %04d format string, which gives me an output of 4970000 - not quite what I need.
Am using the FormatIntoString.vi, which has a "format string" input, but values which should work like "%04d" do not give me the leading zeros.
Ideas? Thx!
Message 1 of 14
(27,408 Views)
If you use the "Format into String" function, there is a wizard that you can access by right clicking this function. In this wizard, is a means by which you can specify padding with zeroes.
Message 2 of 14
(27,404 Views)
%04d is the correct format string for what you're tring to do. Can you post your VI so that someone can look it over to see why it's not working?
Message 3 of 14
(27,404 Views)
It doesn't seem to work - as example if I input a string input "139" and use a format string of %04d I get a resultant of 1390000 not quite the 0139 I would expect. Labview 6.1.
I even tried this in a new VI with the standard functions and terminals, using FormatIntoStrings.VI - it works the same with the %04d format strings. Something obviously wrong..
Any other ideas? Thanks!
0 Kudos
Message 4 of 14
(27,404 Views)
Here is the sample VI.. very simple..
0 Kudos
Message 5 of 14
(27,407 Views)
The format string works on the numeric input of the function. The string input is for adding a string prefix to the output and is not a required input. In your example, if you wire the string to a Scan From String function and wire the output of that to the numeric input of the Format Into String, everything will work just fine. I've attached a corrected VI.
Message 6 of 14
(27,407 Views)
Thank you, Dennis ..
This is exactly what I was after. Obviously I don't understand what's happening inside the Scan From String.VI and Format into String.VI. Have to work at figuring them out.
Thank you again!
Dave
0 Kudos
Message 7 of 14
(27,407 Views)
Hey Dave, you actually don't need a "scan from string.vi" in your program. All you need is a format into string.vi with two string inputs. I've attached my version
0 Kudos
Message 8 of 14
(27,407 Views)
Except that it doesn't work if you are trying to create a string with always 4 characters or some other fixed width. Using the format function with 1234, you get 1234. With 123, you get 0123. With 12, you get 0012. With 1, you get 0001. Your example works only for the third case where the initial string is 2 characters long. You could modify your example to test the string length first and pad with 0, 1, 2, or 3 leading zeroes in a case statement, but that's a lot more work than letting a single function do it for you.
0 Kudos
Message 9 of 14
(27,407 Views)
I have been strugling with leading zeros as well.  Your TestFormat-fixed.vi works perfectly for decimal strings, any idea of how to make it work for hexidecimal strings?
0 Kudos
Message 10 of 14
(27,176 Views)