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 !
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
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 !
real_to_string
int_TO_STRING
ETC
OTHERWISE HAVE A LOOK AT THE OSCAT.LIB
at oscat.de
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,
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.