LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Format string to string with leading zeros

Hello,

I want to format a string to another string with 8 times leading zeros. Format into string is not useful, since I can not give the leading character.

For example I want to convert string "AB" to "000000AB" or "123" to "00000123"

 

If I were doing it in C, I would write so:

printf("%*8s", '0', text);

But in FormatInto String function it is not possible to write this '*'. How can I do it?

 

Thanks.

0 Kudos
Message 1 of 20
(8,010 Views)

You almost had it, just use %08d for the format string.

Format.PNG

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


Message 2 of 20
(8,004 Views)

Find the length of the string, if it is less than 8 then add zeros until the length is 8.

 

Edit1: Kudos to James!

 

Edit2: Wait a second, he said from a string to a string.

0 Kudos
Message 3 of 20
(8,001 Views)

whoops, misread the question, you can ignore this

0 Kudos
Message 4 of 20
(7,998 Views)

Padding with zeroes only works for numeric inputs. It looks like you're converting HEX values, so why not parse the values as HEX numbers instead of a string?

2016-02-26_15-24-11.png

(You could always convert your string to a numeric and then back again into what I've shown above)

 

Failing that, use string length to add however many zeroes you need to the beginning of the string.

2016-02-26_15-26-50.png

 

(there might be a more efficient way to do this, but it's friday afternoon and my brain is being slow)

 

Edit: Here is the slightly less rube-goldbergy version:

2016-02-26_15-29-24.png

 

Edit2: There is an MGI Function 'pad string' but it only allows you to pad at the end of the string...would be nice if you pad at the beginning as well!

 

Edit3: Triple ninja'd!


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 5 of 20
(7,986 Views)

You were actually closer to your answer then you think.

Yes, format into string chooses the characher to be space. But you can easily do a replace afterward with the character you want. (Also it's rather simple VI)

 

String to 8.png

Certified-LabVIEW-Architect_rgb.jpgCertified_TestStand_Architect_rgb.jpg


"I won't be wronged. I won't be insulted. I won't be laid a-hand on. I don't do these things to other people, and I require the same from them." John Bernard Books

0 Kudos
Message 6 of 20
(7,962 Views)

0sfmt.png

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 7 of 20
(7,950 Views)

string.png

Message 8 of 20
(7,946 Views)

I ran a benchmark on the multiple solutions in this thread and there is a very clear winner. We're talking on the order of 1 million iterations, but replacing a subset of 8 zeroes wins by a mile:

Bench 8.PNG

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


Message 9 of 20
(7,933 Views)

@James.M wrote:

I ran a benchmark on the multiple solutions in this thread and there is a very clear winner. Replacing a subset of 8 zeroes wins by a mile:

 


And I learned you can run a For Loop "1M" times instead of "1000000", awesome!
0 Kudos
Message 10 of 20
(7,923 Views)