Trying to convert IPv4 saved as an integer back to a string

I am trying to convert IPv4 addresses from integer to string for display. I found this example code but don’t know how to convert the python right shift and bitwise to Clarion code. The Clarion BSHIFT and BOR /BXOR take longs and I’m not sure the integer fits in the long.

def int_to_ipv4(ip_int):
“”“Converts an integer to an IPv4 address string.”“”
octets =
for i in range(3, -1, -1):
octet = (ip_int >> (i * 8)) & 0xFF
octets.append(str(octet))
return “.”.join(octets)

Example usage

ip_int = 3232235617 # Example integer representing 192.168.1.10
ipv4_address = int_to_ipv4(ip_int)
print(f"Integer: {ip_int}, IPv4 Address: {ipv4_address}")

Explanation:

i * 8: Right shifts the integer by 24, 16, 8, and 0 bits to isolate the octets.

& 0xFF: Performs a bitwise AND with 0xFF (which is 255 in decimal or 11111111 in binary) to mask out all bits except the least significant 8 bits (the octet).

There are a bunch of ways to do what you want. I’ll put a couple here, others will doubtless chip in with others as well;

ip   ULong
g    Group,Over(ip)
a      byte
b      byte
c      byte
d      byte
    End
str   string(16)
  code
  ip = some long
  str = a & '.' & b & '.' & c & '.' & d  ! you might need to test and reverse this order.

Here’s another;

ip   ULong
a      byte
b      byte
c      byte
d      byte
  code
  a = bshift(ip,-24)
  b = bshift(ip,-16)
  c = bshift(ip,-8)
  d = Band(ip,255)
  str = a & '.' & b & '.' & c & '.' & d  ! you might need to test and reverse this order.
1 Like

Thank you Bruce. I used the bshift code and it worked without needing to reverse the join.