LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Padding Zero's Before a String

Solved!
Go to solution

I need to pad a string with zero's on the left.  Example:  2 would be 002, 4F would be 04F.  I am trying to pad just a string, I am using format string and tried %03x but this does not work.  Any suggestions?

-ncm
0 Kudos
Message 1 of 10
(6,036 Views)

Try the concatenate string function. You can use the string length function to determine how many zeros you need to pad with...

 

Strokes

0 Kudos
Message 2 of 10
(6,034 Views)
Solution
Accepted by ncm

The simplest way that comes to mind is to use a for-loop to iterate and concatenate zeros for each "empty space" calculated by Final String Length - Input String Length.  Then concatenate this with the original string.

 

For example, here is something I wrote real quick.  There is probably a more elegant solution, but this should be a good starting point:

 

 

PadZeros.VI

Vi.JPG

 

EDIT: 

 

The above doesn't care what the original input string is.  If you want one more specific to hexadecimal, then you can use something with Format Into String:

 

Padded Hex.vi

 

hex.JPG

The important part of this VI is the format string of "%03x".  The first "0" says to pad the input with zeroes.  The "3" tells you the length of the final string (any strings smaller than that length will get 0 characters to pad it to the requested length).  The "x" tells you it's in hex.

 

 

EDIT 2:

 

The reason "%03x" doesn't worth with String data type inputs is because by default the Format Into String with String data type uses %0 to denote padded spaces.  Either convert your string into a numeric datatype and use the format, or else just use a for-loop as I originally suggested.

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
Download All
Message 3 of 10
(6,020 Views)

Attached is the circuit I have so far.  Essentially what I need is a number that is between 0-4095 (0-FFF in hex) to be converted to a hex number holding 3 places.

Example:

4 --> 004

12 --> 00C

4095 --> FFF

 

-ncm
0 Kudos
Message 4 of 10
(6,019 Views)

I must be missing something; this does what your last post says you need.

 

Example_VI.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 5 of 10
(6,002 Views)

Thanks! That was exactly what I needed!

-ncm
0 Kudos
Message 6 of 10
(5,998 Views)

First of all, it is better to attach your actual VIs.  It is a lot easier to debug that way.

 

Your program doesn't make any sense.  Your rolling number is an integer, yet you expect to get a floating point out of it.

 

All you really need is the Format String with the format being %04x and have your rolling number as the input.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 10
(5,997 Views)

@ncm wrote:

Thanks! That was exactly what I needed!


What was exactly what you needed?  You should mark the post that helped you as the solution (and, liberally give kudos) so that future visitors to this thread can see what answered your question.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 8 of 10
(5,959 Views)

@jcarmody wrote:

@ncm wrote:

Thanks! That was exactly what I needed!


What was exactly what you needed?  You should mark the post that helped you as the solution (and, liberally give kudos) so that future visitors to this thread can see what answered your question.


You're a little late to the party my friend.  He already accepted my solution and gave Kudos haha.  I don't think he is as clueless about the NI Forums as you seem to be suggesting. 😉

 

To clarify, he asked in his OP regarding why "%03x" did not format a hexadecimal string to be padded with zeros, but rather, padded with spaces.  I address this above and give him two possible solutions. 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
Message 9 of 10
(5,949 Views)

@jcarmody wrote:

I must be missing something [...]


Indeed, I certainly was.


@MrHappyAsthma wrote:

 

[...]

You're a little late to the party my friend.  He already accepted my solution and gave Kudos haha.  I don't think he is as clueless about the NI Forums as you seem to be suggesting. 😉

[...]


Now that I actually read your entire post I see that I was out of line.  Even though his post immediately before mine, with the attached block diagram not containing any of your suggestions (even after marking it as the solution) seems to indicate that he was still having trouble, I apologize to you and ncm, offer kudos of my own as propitiation, and repent in sackcloth and ashes.

 

🙂

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 10 of 10
(5,930 Views)