Split int to int array?

Volvo742
2016-06-10
2016-06-22
  • Volvo742 - 2016-06-10

    Hello.

    Is it possible to split int to int array?

    For example int value 5356 to 5, 3, 5, 6?

     
  • yannickasselin1 - 2016-06-10

    You could do it with a FOR loop and some modulo instructions.

     
  • Volvo742 - 2016-06-10

    yannickasselin1 hat geschrieben:
    You could do it with a FOR loop and some modulo instructions.

    Thank you! That solve my problem.

     
  • Anonymous - 2016-06-12

    Originally created by: scott_cunningham

    Additionally, you can use INT_STRING and then grab each char and use STRING_TO_INT until end of string.

     
  • Volvo742 - 2016-06-12

    Thank you. You men i use indexing?

    Example :
    var2: INT_STRING := 6367;
    var1: STRING;

    var1 := INT_TO_STRING (var2 [0]); // first number

     
  • Anonymous - 2016-06-22

    Originally created by: scott_cunningham

    Sorry Volvo742, I never get emails from this BBL anymore. Something blocks them.

    I made a typing mistake - I meant to use INT_TO_STRING and then iterate through each char of the string and go STRING_TO_INT. Like this:

    PROGRAM PLC_PRG
    VAR
       Start : INT := 5432;
       Result : ARRAY[0..9] OF INT;
    END_VAR
    VAR_TEMP
       J : INT;
       Str : STRING;
    END_VAR
    Str := INT_TO_STRING(Start);
    FOR J := 0 TO LEN(Str) DO
       Result[J] := STRING_TO_INT(MID(Str,1,J+1));
    END_FOR
    

    Of course, putting this as a FUNCTION would be better...

    A little faster FOR LOOP solution (MID may be slow and one shouldn't put any calculations in the FOR line as it calculates every time it loops):

    PROGRAM PLC_PRG
    VAR
       Start : INT := 5432;
       Result : ARRAY[0..9] OF INT;
    END_VAR
    VAR_TEMP
       J : INT;
       Str : STRING;
       Chars : INT;
    END_VAR
    Str := INT_TO_STRING(Start);
    Chars := LEN(STR) - 1;
    FOR J := 0 TO Chars DO
       Result[J] := BYTE_TO_INT(Str[J]-48);
    END_FOR
    

    Indexing in a string returns a byte data type, which happens to be the ascii code.

     

Log in to post a comment.