Search code examples
hexasciicodesys

How to get char from a Byte ASCII value in Codesys


Good morning,

I would like to get the char which corresponds to a ASCII code. For example, if I have a byte with a value 16#68, I would like to get a char with value 'h'.

Thanks!


Solution

  • Codesys 3.5

    VAR
        someByte: BYTE := 16#68;
        theChar: STRING(1);
    END_VAR
    

    theChar[0] := someByte;

    enter image description here

    A STRING is just an array of BYTES. You can replace any of them with whatever value you want.

    Alternatives:

    VAR
        someByte: BYTE := 16#68;
        theChar: STRING(1);
        bytePtr: POINTER TO BYTE := ADR(theChar);
    END_VAR
    

    bytePtr[0] := someByte; or bytePtr^ := someByte;

    Or

    Create a Union:

    TYPE CHAR :
    UNION
        ascii: STRING(1);
        raw: BYTE;
    END_UNION
    END_TYPE
    

    theChar.raw := 16#68;

    enter image description here