# Convert int to BCD data

**URL:** <https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697>\
**Category:** Ignition\
**Created:** [May 19, 2016, 11:39am UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697 "2016-05-19T11:39:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgarcia](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@lgarcia](https://forum.inductiveautomation.com/u/lgarcia)\
**Post date:** [May 19, 2016, 11:39am UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697/1 "2016-05-19T11:39:10Z")

</div>

[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](https://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,

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 19, 2016, 12:10pm UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697/2 "2016-05-19T12:10:35Z")

</div>

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(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0\_92  
Type "help", "copyright", "credits" or "license" for more information.

> > > 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]

---

<div class="post-metadata">

**Author:** ![lgarcia](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@lgarcia](https://forum.inductiveautomation.com/u/lgarcia)\
**Post date:** [May 19, 2016, 12:35pm UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697/3 "2016-05-19T12:35:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 19, 2016, 3:57pm UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697/4 "2016-05-19T15:57:13Z")

</div>

EDIT: Phil already covered this in his post with:

```auto
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
  `

---

<div class="post-metadata">

**Author:** ![lgarcia](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@lgarcia](https://forum.inductiveautomation.com/u/lgarcia)\
**Post date:** [May 19, 2016, 5:05pm UTC](https://forum.inductiveautomation.com/t/convert-int-to-bcd-data/11697/5 "2016-05-19T17:05:59Z")

</div>

Hi Jordan,

Thanks!!!

You saved me…!!!

Thanks Again.  
Regards,
