adding a 0 at the start of the string
satyrux
Posts: 7
Hello I have a problem, I retrieve a position of a camera the value is between 100 and 1600 the problem is that from 100 to 999 there must be a 0 in front of 0150, I tried with format but I cannot can only get a result of 48
volatile char test = 0150 (camera return value)
volatile char ntest
ntest = format('%04d',test)
Do you have any idea what could be the problem?
0
Comments
Hello,
The FORMAT function takes a number as an input and gives a string as an output. It does basically the same thing as ITOA but on steroids.
In your example it seems you assume the opposite of that. That would not make that much sense cause a number 0150 would still be the same number as 150.
You probably want a string (not a number) to send back to the camera and if it's length is shorter than 4 it has to be prepended with zero's ('0150' instead of '150')
In the below example there are two methods for that. FormatTest1 is more universal, FormatTest2 is just an example of another way to do that. I would use the former.
It takes the return value string from the camera, converts that into an integer (number) and than uses FORMAT to give back a formatted string with the correct length.
Hello I have just tested solution 1 and indeed, the error I made was that I was looking at the value of the result variable, but by checking with the sending of the string I have my value with a 0 in front. Thank you again for your help and the time spent for the solution