# Label - Expression to conditionally format Hex value

**URL:** <https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621>\
**Category:** Ignition\
**Created:** [July 7, 2022, 12:03pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621 "2022-07-07T12:03:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Tryon](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Richard\_Tryon](https://forum.inductiveautomation.com/u/Richard_Tryon)\
**Post date:** [July 7, 2022, 12:03pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/1 "2022-07-07T12:03:08Z")

</div>

I am extracting the PLC time. It is stored in registers where the 16 bit register represents 2 BCD numbers. In the example here it is for Day\_Hour.  
Problem is when the day or hour is less than 10decimal, Ignition only shows 1 digit. So Day of month = 07 returns 7, and not 07.  
I am trying to have the Label show two digits for BCD regardless of what the value is. So regardless of the day, it will show 07, or 12, or 31 etc.  
How can I do this in the expression for the Label or should I do this somewhere else?  
In the attached screen shot, it shows how what I am currently doing in the expression is not working because all I’m getting back from the ‘toHex’ funcion is 707, whereas I need to display 07 07 (7th day and the 7th hour)

```auto

left(toHex({KasaTMMC/Final4/F4-TL/P1_S001A}),2) + " : " + right(toHex({KasaTMMC/Final4/F4-TL/P1_S001A}),2)

```

![Hex datetime](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/e/ce57df68a8470fede59966e732dabed5901668e2.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 7, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/2 "2022-07-07T18:02:56Z")

</div>

This expression will do the trick:

```auto
stringFormat('%02X:%02X',
    ({KasaTMMC/Final4/F4-TL/P1_S001A} >> 8), 
    ({KasaTMMC/Final4/F4-TL/P1_S001A} & 255)
)

```

An explanation of how this works.

1. The `stringFormat` expression accepts format strings which utilizes the Java String Format Syntax, found [here](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html). Esentially, this string is saying it will take 2 arguments and format them both as a Hexadecimal Integer with zero padding to a width of 2 characters.

2. This: `{tag} >> 8` performs a right bit shift to the value of tag 8 bits to the right. It helps to think of the numbers in binary. In this instance the binary value would be `0b0000 0111 0000 0111`, following the bit shift the resultant value is `0b0000 0000 0000 0111`.

3. This: `{tag} & 255` performs a bitwise AND operation between the value of tag and 255. Again it helps to think of the numbers in binary. In this instance:  
`0b0000 0111 0000 0111 & 0b0000 0000 1111 1111 = 0b0000 0000 0000 0111`

The resultant string will be `07:07`

---

<div class="post-metadata">

**Author:** ![Richard\_Tryon](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Richard\_Tryon](https://forum.inductiveautomation.com/u/Richard_Tryon)\
**Post date:** [July 8, 2022, 11:10am UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/3 "2022-07-08T11:10:16Z")

</div>

> [@lrose](#):
>
> ```auto
> stringFormat('%02X:%02X',
> ({KasaTMMC/Final4/F4-TL/P1_S001A} >> 8), 
> ({KasaTMMC/Final4/F4-TL/P1_S001A} & 255)
> )
> 
> ```

That did it, and thank you for the wonderful explanation!

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 8, 2022, 11:13am UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/4 "2022-07-08T11:13:57Z")

</div>

I’m sorry but, why hex ?  
If all you want is to pad the numbers with `0`s, hex has nothing do to with it.

I’ve never used BCD (why does this even still exist ?) with ignition so I have no idea how the values are interpreted, if it’s built-in or if you need to interpret them yourself, but I’d probably try to convert your `Min_Sec` and `Day_Hour` into a datetime object, then use this with whatever date formatting you may need.  
Using expressions, you’d be looking at `getDate()` and `setTime()`, maybe something like

```auto
setTime(getDate(0, 0, {Day_Hour} >> 8), {Day_Hour} & 0xff, {Min_Sec} >> 8, {Min_Sec} & 0xff)

```

If you don’t want to create a datetime object because you only ever need to display `dd hh:mm:ss`, then take @lrose’s expression. But I still don’t see the point of using hex conversion.

edit:  
Wait, no, this is wrong. I forgot it was BCD, simply shifting 8 to the right won’t do the trick, as the 2 ‘nibbles’ (a nibble being half a byte) will be interpreted as an 8 bit integer.  
Let me think about it and come back with a better solution.

re edit: Can you try lrose’s expression with numbers higher than 9 ? I’m expecting 07 to work fine because the 0 nibble is still 0, and 7 is still 7. But 11 (0001 0001) would be interpreted as 17, 12 as 18, etc…

re re edit: converting 4 BCD numbers encode into 16 bits to two 8 bits integers would look something like

```python
left_byte = bcd >> 8
first_number = (left_byte >> 4) * 10 + (left_byte & 0x0F)

right_byte = bcd & 0xFF
second_number = (right_byte >> 4) * 10 + (right_byte & 0x0F)

```

Doing it with an expression would look quite ugly I think, so I’d make a function for this.  
Then I still believe that using those to create a datetime object is not a bad idea.

---

<div class="post-metadata">

**Author:** ![Richard\_Tryon](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Richard\_Tryon](https://forum.inductiveautomation.com/u/Richard_Tryon)\
**Post date:** [July 8, 2022, 11:45am UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/5 "2022-07-08T11:45:20Z")

</div>

> [@pascal.fragnoud](#):
>
> I’ve never used BCD (why does this even still exist ?)

It's a Japanese plc system and this is how they handle their time clocks. It is actually HEX, but we just 'view' the number displayed in the programming software, or on the HMI as a BCD number. I guess the word BCD shouldn't be used, it's just that through the plc software, these clock registers will never see a value beyond that what you would see in a 'BCD' clock.  
My bad, I should have explained this a bit better.

As of 07:29AM this morning, the solution works (I applied it to the minutes\_seconds plc registers as well.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/3/63ad5f719080378c366a866dd5def4b8fc8648f9.png)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 8, 2022, 12:00pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/6 "2022-07-08T12:00:21Z")

</div>

So you’re getting two 8 bits integers at a time, encoded into a 16 bits value, from the plc ?

---

<div class="post-metadata">

**Author:** ![Richard\_Tryon](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Richard\_Tryon](https://forum.inductiveautomation.com/u/Richard_Tryon)\
**Post date:** [July 8, 2022, 12:24pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/7 "2022-07-08T12:24:41Z")

</div>

They are 2 x 8 bit integers yes, but from an interpretation stand point, they are to be viewed on any system, as HEX (but I think of these particular ones as BCD since I am old school and they are representing clock values)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/3/233c9739d85c0aafd5fabafbfdb9d793e2a8f2e6.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 8, 2022, 12:26pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/8 "2022-07-08T12:26:06Z")

</div>

Yes.

BCD is very wasteful because each nibble can only be 0-9.

However, just using the integer value won’t work because, in the PLC it is stored directly as HEX.

So for instance in the PLC a value might be stored as `0x2920` that value in Ignition would be 10528. As you can see not as readable.

I agree if you were going to try and store this value for use elsewhere in the view/application then it makes since to store it as a date/time. However if you only want to display it as a string no use converting it to a date just to convert it back to a string.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 8, 2022, 12:48pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/9 "2022-07-08T12:48:54Z")

</div>

Ooooh so you mean 29 mins and 20 seconds is stored as 0x2920 ? That would make it actual BCD, and everything makes sense !

---

<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:** [July 8, 2022, 12:55pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/10 "2022-07-08T12:55:16Z")

</div>

Omron, in particular, is fond of BCD.

---

<div class="post-metadata">

**Author:** ![harold.bruulsema](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@harold.bruulsema](https://forum.inductiveautomation.com/u/harold.bruulsema)\
**Post date:** [July 12, 2022, 12:23pm UTC](https://forum.inductiveautomation.com/t/label-expression-to-conditionally-format-hex-value/61621/11 "2022-07-12T12:23:35Z")

</div>

Allen-Bradley PLC-2 used BCD, by default if I recall, although I think you could also use decimal.  
ControlLogix still has TOD and FRD instructions to convert to/from BCD.
