 # Convert int to BCD data

[color=#0000FF][color=#0000FF][/color][/color]Hi,

I neet to convert Int to BCD Data. I found a python library bcd 0.4.( check the link) pypi.python.org/pypi/bcd/0.4
Is that possible to download this library in Ignition? How can I do that?

If not, there is any code that someone is using to convert bin, hex, and decimal to BCD???

Regards,

You can take advantage of the fact that BCD is encoded the same as those digits in hexadecimal. So printing the bcd register (an integer) as hex will display it correctly. Converting that hex string as if it is decimal then gives you the normal integer. Some examples:[code]Jython 2.5.3 (, Sep 23 2013, 00:05:54)
[Java HotSpot™ 64-Bit Server VM (Oracle Corporation)] on java1.8.0_92

bcd = 0x456
bcd
1110

hex(bcd)
‘0x456’

‘%x’ % bcd
‘456’

decimal = int(’%x’ % bcd)
decimal
456

bcd2 = int(str(decimal), 16)
bcd2
1110

hex(bcd2)
‘0x456’

[/code]

I’m sorry but I can’t understand.

I need to write a decimal value. Example. 14.

this number in BCD means 0001 0100. Every decimal digit means four bits.

Example: Decimal 1 = BCD 0001
Decimal 4 = BCD 0100
I need a code to make that convertion. I have no idea how to building this code…Sorry but I’m not a deep python or Java programmer.

Regards,
Leandro

EDIT: Phil already covered this in his post with:

``bcd = int(str(decimal), 16)``

a much shorter method… This is one I wrote a while back:

```def dec2BCD(inputValue): x=str(inputValue) BCDOut = 0 for char in x: BCDOut = (BCDOut << 4) + int(char) return BCDOut ```

Hi Jordan,

Thanks!!!

You saved me…!!!

Thanks Again.
Regards,