Last active
November 17, 2017 19:17
-
-
Save RyanBreaker/d6c08fc4ebaf986119f8d0a110eaf91a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| extension UInt32 { | |
| public func IPv4String() -> String { | |
| let byte1 = UInt8(self & 0xff) | |
| let byte2 = UInt8((self >> 8) & 0xff) | |
| let byte3 = UInt8((self >> 16) & 0xff) | |
| let byte4 = UInt8((self >> 24) & 0xff) | |
| return "\(byte1).\(byte2).\(byte3).\(byte4)" | |
| } | |
| } | |
| extension String { | |
| public func IPv4Int() -> UInt32 { | |
| var addr = in_addr() | |
| let retval = inet_pton(AF_INET, self, &addr) | |
| if retval == 1 { return addr.s_addr } | |
| return 0 | |
| } | |
| } | |
| let address = "192.168.0.1" | |
| address.IPv4Int().IPv4String() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment