ASCII

thebig1
2013-01-21
2013-02-11
  • thebig1 - 2013-01-21

    Hi All,

    I am quite new to CodeSys and have a queestion.

    I need to convert a Decimal or Hex to ASCII.

    Is there a simple way of doing it?

     
  • JAPIB

    JAPIB - 2013-01-22

    To convert a dΓ©cimal digit to ASCII, you have to ADD 50 to your decimal digit.
    For example : 5 (decimal digit) --> 5 + 50 = 55
    55 is decimal ASCII code for decimal digit 5

    To convert a hexadigit to ASCII, you have to ADD 16#30 to your decimal digit.
    For example : 5 (decimal digit) --> 5 + 30 = 35
    16#35 is hexa ASCII code for decimal digit 5

    To convert a number you must isolate each digit includes the number, then add 50 or 16#30 to each isolate digit

    For example to convert VAL1 to ASCII code :

    VAR
    Val1:UINT:=24;
    VAL2, Dec_ASCII,Unit_ASCII : UINT;
    END_VAR


    (Program)

    (Decade)
    Val2:=Val1/10 (result =2, decade)

    (ASCII code for decade)
    Dec_ASCII:= Val1 + 50 = 52 (ASCII code for "2" )

    (ASCII code for unit)
    Unit_ASCII:= (Val1-(10 x Val2)) + 50 = 4 + 50 = 54 (ASCII code for "4", unit)


    In this example, Val1 is only composed of 2 digits.
    The program will be longer if val1 is more long !

     
  • shooter - 2013-01-22

    real_to_string
    int_TO_STRING
    ETC
    OTHERWISE HAVE A LOOK AT THE OSCAT.LIB
    at oscat.de

     
  • thebig1 - 2013-01-22

    Thanks for that.

    Is there anyway to show the careracter of the ASCII code?
    For example, I need to send "SMFHD4BCOCGKS1508" over CAN
    So I have to convert the whole of it to DEC first, then send via CAN as BYTES.
    At the other end I have to recieve the CAN BYTEs, convert to DEC, then back to the ASCII caraters.

    Cheers,

     
  • erik.gerrits - 2013-02-11

    To convert decimal to ASCII you can also use a pointer. I have not tried to use a pointer to convert ASCII to decimal but I would imagine it would be the same thing since you are just pointing to a memory location and pulling the data in the type that you need it.

     

Log in to post a comment.