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

**URL:** <https://clarionhub.com/t/trying-to-convert-ipv4-saved-as-an-integer-back-to-a-string/8226>\
**Category:** questions\
**Tags:** network, math\
**Created:** [May 27, 2025, 3:52am UTC](https://clarionhub.com/t/trying-to-convert-ipv4-saved-as-an-integer-back-to-a-string/8226 "2025-05-27T03:52:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidA](https://clarionhub.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@DavidA](https://clarionhub.com/u/DavidA)\
**Post date:** [May 27, 2025, 3:52am UTC](https://clarionhub.com/t/trying-to-convert-ipv4-saved-as-an-integer-back-to-a-string/8226/1 "2025-05-27T03:52:34Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Bruce](https://clarionhub.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Bruce](https://clarionhub.com/u/Bruce)\
**Post date:** [May 27, 2025, 5:52am UTC](https://clarionhub.com/t/trying-to-convert-ipv4-saved-as-an-integer-back-to-a-string/8226/2 "2025-05-27T05:52:09Z")

</div>

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;

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

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

```

---

<div class="post-metadata">

**Author:** ![DavidA](https://clarionhub.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@DavidA](https://clarionhub.com/u/DavidA)\
**Post date:** [May 27, 2025, 11:56pm UTC](https://clarionhub.com/t/trying-to-convert-ipv4-saved-as-an-integer-back-to-a-string/8226/4 "2025-05-27T23:56:58Z")

</div>

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