Hi! Is there a way to convert CHAR ARRAY into INTEGER. I have an array of CHAR that contains only number and I need to put the entire number stored into the array into an Integer...
![](https://www.robot-forum.com/images/avatars/ef/85-ef99d0a4de6ad799431c2114c7d14d905cd951d6.png)
CHAR to INT in KRL
-
IlFincoITA -
April 5, 2023 at 7:15 AM -
Thread is Unresolved
-
-
Post the array value as well as what is supposed be after conversion
-
SREAD function would be one way. Probably the simplest.
-
This is what I need to convert. I receive the order number on Profinet Bus mapped on 8 bytes:
OrderNo[1]
OrderNo[2]
OrderNo[3]
OrderNo[4]
OrderNo[5]
OrderNo[6]
OrderNo[7]
OrderNo[8]
Since OrderNo's CHARs are only numbers I need to convert it into INT
-
If I understood what you want, then StrToInt() should do the trick.
-
yup... if you have values already in char array and in correct order, that is all that is needed.
but if the value is on group of inputs, different approach would be needed.
a not so elegant but simple solution in this case is to declare eight 8-bit signals, copy their values to character array one by one and then do conversion.
-
btw one other option is to use SREAD, this allows converting up to 8 values at once.
-
This is what I need to convert. I receive the order number on Profinet Bus mapped on 8 bytes:
OrderNo[1]OrderNo[2]
OrderNo[3]
OrderNo[4]
OrderNo[5]
OrderNo[6]
OrderNo[7]
OrderNo[8]
Since OrderNo's CHARs are only numbers I need to convert it into INTSo, what does the final order number look like? An 8-digit numeric value? Alphanumeric? Other? Any special characters? Decimal points?
A very simplistic example using SWRITE:
CodeDECL INT _nOffset, _nIndex DECL STATE_T _State DECL CHAR _chOrderNum[8] StrClear (_chOrderNum[]) _nOffset=0 FOR _nIndex = 1 TO 8 SWRITE (_chOrderNum[], _State, _nOffset, "%d", OrderNo[_nIndex]) ENDFOR
This probably isn't quite correct, I don't have a system to test it on. But it should be basically correct. _nOffset is how the SWRITE keeps track of the "end" of the string.
_State is something SWRITE requires, but I honestly don't recall what it's for, I never actually used it for anything.
"%d" tells SWRITE how to "translate" the non-string variable (OrderNo[_Index] in this case) into a CHAR/string. %d means the variable should be treated as an Integer.