Bitshift of int to get value of byte[X]

Hello,

I have a value (integer) from sensor (modbus)
I need now the 4 bytes from the integer to get some information.

Example:

integer x = 825307441;
Byte-Convert the integer x ToByte[] = [ “49”, “49”, “49”, “49” ]

How can i do this in HA?
I need a Bitshift or Bitconverter i think.

Thanks

Here is something I came up with. You can copy-paste into Developer Tools-> Templates and play with it.
A bit shift to the right is achieved by dividing a number by 2**n where n is the number of bits to shift.

{% set num = 825307441 %}
{% set num1= (num/2**0)  | int | bitwise_and(255) %}
{% set num2= (num/2**8)  | int | bitwise_and(255) %}
{% set num3= (num/2**16)  | int | bitwise_and(255) %}
{% set num4= (num/2**24)  | int | bitwise_and(255) %}
{% set ToByte =  num1, num2, num3, num4 %}
{{ ToByte[0], ToByte[1], ToByte[2], ToByte[3] }}
2 Likes