# Tag values - converting 16-bit integer to two 8-bit integers

**URL:** <https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [April 27, 2021, 10:47pm UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087 "2021-04-27T22:47:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![levi\_jacobs](https://avatars.discourse-cdn.com/v4/letter/l/ba9def/32.png) [@levi\_jacobs](https://forum.inductiveautomation.com/u/levi_jacobs)\
**Post date:** [April 27, 2021, 10:47pm UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/1 "2021-04-27T22:47:42Z")

</div>

I’m wondering if there is an easy way to convert a 16-bit integer tag value into two 8-bit tag values (one for the high byte, one for the low byte).

Here’s how i’ve been doing it:

1. I have an integer value coming into an OPC tag.
2. I make an expression tag that converts the integer into a binary string using the **toBinary** function. The **toBinary** function seems to strip leading zeros, so I also have some code to pad the binary string with zero’s to make it 16 bits long.
3. I make 2 more expression tags. One tag reads the first 8 characters/bits of the binary string, the other reads the second 8 characters/bits. Now I have the high and low bytes of my original integer stored in 2 separate tags, as binary strings.
4. I make 2 more expression tags. I use the **fromBinary** function to convert the two 8-bit binary strings into integers.

Anyone know of a more straight forward way to accomplish this?

Note: I know I can consolidate some of the expression tags, it’s just easier to explain my question this way.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/d/6d551149e8655d8a8962765391bd147f4082942f.png)

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [April 28, 2021, 12:08am UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/2 "2021-04-28T00:08:13Z")

</div>

Probably not even close to the best way, but run this script on value change:

```auto
import struct
a = 4634
s = struct.pack('>H', a)

first, second = struct.unpack('>BB', s)
print first
print second

```

I’m curious to know the need? Packing bits was a real need back in the PLC5 days, but today?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 28, 2021, 12:22am UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/3 "2021-04-28T00:22:40Z")

</div>

You guys are overthinking this.

High Byte: `({IntegerTag} >> 8) & 0xFF`  
Low Byte: `{IntegerTag} & 0xFF`

---

<div class="post-metadata">

**Author:** ![levi\_jacobs](https://avatars.discourse-cdn.com/v4/letter/l/ba9def/32.png) [@levi\_jacobs](https://forum.inductiveautomation.com/u/levi_jacobs)\
**Post date:** [April 28, 2021, 2:17am UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/4 "2021-04-28T02:17:17Z")

</div>

Wow haha… Thanks.

---

<div class="post-metadata">

**Author:** ![Billy\_Elliott](https://avatars.discourse-cdn.com/v4/letter/b/c37758/32.png) [@Billy\_Elliott](https://forum.inductiveautomation.com/u/Billy_Elliott)\
**Post date:** [September 14, 2022, 1:54pm UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/5 "2022-09-14T13:54:33Z")

</div>

> [@Kevin.Herron](#):
>
> ({IntegerTag} \>\> 8) & 0xFF

Is it possible to do that conversion in the original tag definition or do you have to pull it into a tag and then create an expression tag to do the conversion. It would be a lot cooler to do it all in one tag

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 14, 2022, 1:55pm UTC](https://forum.inductiveautomation.com/t/tag-values-converting-16-bit-integer-to-two-8-bit-integers/46087/6 "2022-09-14T13:55:57Z")

</div>

You need a separate tag. The Modbus driver doesn’t have byte-level addressing because registers are 16-bit.
