Skip to content

Instantly share code, notes, and snippets.

@cybertk
Created August 17, 2015 07:55
Show Gist options
  • Save cybertk/61dbc56595723efb7fac to your computer and use it in GitHub Desktop.
Save cybertk/61dbc56595723efb7fac to your computer and use it in GitHub Desktop.

Revisions

  1. cybertk created this gist Aug 17, 2015.
    24 changes: 24 additions & 0 deletions UInt32+IPv4String.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    //
    // UInt32+IPv4String.swift
    // Cybertk
    //
    // Created by Quanlong He on 8/14/15.
    // Copyright © 2015 Quanlong He. All rights reserved.
    //

    import Foundation

    extension UInt32 {

    public func IPv4String() -> String {

    let ip = self

    let byte1 = UInt8(ip & 0xff)
    let byte2 = UInt8((ip>>8) & 0xff)
    let byte3 = UInt8((ip>>16) & 0xff)
    let byte4 = UInt8((ip>>24) & 0xff)

    return "\(byte1).\(byte2).\(byte3).\(byte4)"
    }
    }