BCD_TO_INT / INT_TO_BCD¶
Convert between Binary-Coded Decimal (BCD) encoded bit strings and integer values.
Signature¶
BCD_TO_INT:
┌─────────────┐
IN ─┤ BCD_TO_INT ├─ OUT
└─────────────┘
FUNCTION BCD_TO_INT : ANY_INT
VAR_INPUT
IN : ANY_BIT;
END_VAR
END_FUNCTION
BCD_TO_INT accepts BYTE, WORD, DWORD, LWORD for
IN; the return type is the corresponding unsigned integer (USINT,
UINT, UDINT, ULINT).
INT_TO_BCD:
┌─────────────┐
IN ─┤ INT_TO_BCD ├─ OUT
└─────────────┘
FUNCTION INT_TO_BCD : ANY_BIT
VAR_INPUT
IN : ANY_INT;
END_VAR
END_FUNCTION
INT_TO_BCD accepts USINT, UINT, UDINT, ULINT for
IN; the return type is the corresponding bit string (BYTE,
WORD, DWORD, LWORD).
Inputs (BCD_TO_INT)
Name |
Type |
Description |
|---|---|---|
|
|
The BCD-encoded bit string to decode. |
Outputs (BCD_TO_INT)
Name |
Type |
Description |
|---|---|---|
Return value |
|
The decoded integer value. Type is the unsigned integer corresponding to the width of IN. |
Inputs (INT_TO_BCD)
Name |
Type |
Description |
|---|---|---|
|
|
The unsigned integer value to encode. |
Outputs (INT_TO_BCD)
Name |
Type |
Description |
|---|---|---|
Return value |
|
The BCD-encoded bit string. Type is the bit string corresponding to the width of IN. |
Description¶
BCD (Binary-Coded Decimal) encodes each decimal digit in a 4-bit nibble.
For example, the decimal value 42 is encoded as 0100_0010 in BCD
(4 in the high nibble, 2 in the low nibble).
BCD_TO_INT decodes a BCD-encoded bit string into its integer value.
The function treats invalid BCD nibbles (values 10–15) as 0.
INT_TO_BCD encodes an integer value into BCD format. Values that
exceed the maximum representable BCD value for the target width wrap
around.
Maximum values per width:
BYTE: 99WORD: 9999DWORD: 99999999LWORD: 9999999999999999
Example¶
int_val := BCD_TO_INT(BYTE#16#42); (* int_val = 42 *)
bcd_val := INT_TO_BCD(USINT#42); (* bcd_val = 16#42 *)
See Also¶
Type Conversions — other type conversion functions