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.
PROGRAM_NAME='FormatTest' (***********************************************************) (* FILE CREATED ON: 05/25/2024 AT: 11:44:25 *) (***********************************************************) (* FILE_LAST_MODIFIED_ON: 05/25/2024 AT: 12:31:08 *) (***********************************************************) (***********************************************************) (* VARIABLE DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_VARIABLE Volatile char cTest1[] = '150' Volatile char cTest2[] = '1550' Volatile char cTest3[] = '7' Volatile char cTest11[4] Volatile char cTest12[4] Volatile char cTest13[4] (***********************************************************) (* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW *) (***********************************************************) define_function char[4] FormatTest1(char cInput[]) { stack_var nInput If (Atoi(cInput) <> 0) { nInput = atoi(cInput) return format('%04d',nInput) } } define_function char[4] FormatTest2(char cInput[]) { If (length_string(cInput) < 4) { //assuming the only other length = 3 (100...999) return "'0',cInput" } else { return cInput } } (***********************************************************) (* STARTUP CODE GOES BELOW *) (***********************************************************) DEFINE_START wait 100 //time to turn on 'diagnostics'... { cTest11 = FormatTest1(cTest1) send_string 0, "'cTest11: ',cTest11" cTest12 = FormatTest1(cTest2) send_string 0, "'cTest12: ',cTest12" cTest13 = FormatTest1(cTest3) send_string 0, "'cTest13: ',cTest13" //second example cTest11 = FormatTest2(cTest1) send_string 0, "'cTest11: ',cTest11" cTest12 = FormatTest2(cTest2) send_string 0, "'cTest12: ',cTest12" cTest13 = FormatTest2(cTest3) //this will fail as the input has length 1, not 3 send_string 0, "'cTest13: ',cTest13" } DEFINE_PROGRAM (*****************************************************************) (* END OF PROGRAM *) (* *) (* !!! DO NOT PUT ANY CODE BELOW THIS COMMENT !!! *) (* *) (*****************************************************************)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