Swift:如何将 IPv4 字符串地址转换为整数,反之亦然

发布于 2025-01-20 05:26:50 字数 440 浏览 3 评论 0原文

我需要将 IP 地址(例如“127.0.0.1”)转换为整数值,反之亦然。我找到了 ObjC 的一些示例:

如何在 Swift 中执行此操作最好的方法是什么?

I need to convert IP address (e.g. "127.0.0.1") to integer value and vice-versa for my logger. I've found some samples for ObjC:

How to do it in Swift and what the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2025-01-27 05:26:50

这就是我处理转换的方式。它可能看起来有点过度工程,但所有属性在其他上下文中也很有用:

extension Numeric {
    var data: Data {
        var bytes = self
        return Data(bytes: &bytes, count: MemoryLayout<Self>.size)
    }
}

extension Data {
    func numeric<T: Numeric>() -> T { withUnsafeBytes { $0.load(as: T.self) } }
}

extension LosslessStringConvertible {
    var string: String { .init(self) }
}

以下实现依赖于网络技术:


import Network

extension StringProtocol {
    var ipV4Address: IPv4Address? { .init(string) }
}

extension Data {
    var ipV4Address: IPv4Address? { .init(self) }
}

extension IPv4Address {
    var uint32: UInt32 { rawValue.numeric() }
}

extension UInt32 {
    var ipV4Address: IPv4Address? { data.ipV4Address }
}

用法:

if let ipV4Address = "10.0.0.1".ipV4Address {  // 10.0.0.1
    let uint32 = ipV4Address.uint32  // 16777226
    let loadedIPv4Address = uint32.ipV4Address!  // 10.0.0.1
}

This is how I would approach the conversion. It might look a bit over engineering but all properties are useful in other contexts as well:

extension Numeric {
    var data: Data {
        var bytes = self
        return Data(bytes: &bytes, count: MemoryLayout<Self>.size)
    }
}

extension Data {
    func numeric<T: Numeric>() -> T { withUnsafeBytes { $0.load(as: T.self) } }
}

extension LosslessStringConvertible {
    var string: String { .init(self) }
}

The following implementations rely on Network Technology:


import Network

extension StringProtocol {
    var ipV4Address: IPv4Address? { .init(string) }
}

extension Data {
    var ipV4Address: IPv4Address? { .init(self) }
}

extension IPv4Address {
    var uint32: UInt32 { rawValue.numeric() }
}

extension UInt32 {
    var ipV4Address: IPv4Address? { data.ipV4Address }
}

Usage:

if let ipV4Address = "10.0.0.1".ipV4Address {  // 10.0.0.1
    let uint32 = ipV4Address.uint32  // 16777226
    let loadedIPv4Address = uint32.ipV4Address!  // 10.0.0.1
}
忆沫 2025-01-27 05:26:50

在 Swift 中,有两种可能的方法:

  1. 使用老式的 inet_atoninet_ntoa
func ipv4_StringToInt(_ value: String) -> UInt32? {
    var addr = in_addr()
    return inet_aton(value.cString(using: .ascii), &addr) != 0
        ? UInt32(addr.s_addr)
        : nil
}

func ipv4_IntToString(_ value: UInt32) -> String {
    let addr = in_addr(s_addr: value)
    return String(cString:inet_ntoa(addr))
}
  1. 使用 Network 中的 IPv4Address
func ipv4_StringToInt(_ value: String) -> UInt32? {
    IPv4Address(value)?.rawValue.withUnsafeBytes {
        $0.load(as: UInt32.self)
    }
}

func ipv4_IntToString(_ value: UInt32) -> String {
    let data = withUnsafeBytes(of: value) { Data($0) }
    return IPv4Address(data)!.debugDescription
}

如何使用:

if let addr = ipv4_StringToInt("127.0.0.1") { // addr = 0x100007F
    let ip = ipv4_IntToString(addr)
    print(ip) // Outputs: 127.0.0.1
}

There two possible approaches to do it in Swift:

  1. Using old school inet_aton and inet_ntoa
func ipv4_StringToInt(_ value: String) -> UInt32? {
    var addr = in_addr()
    return inet_aton(value.cString(using: .ascii), &addr) != 0
        ? UInt32(addr.s_addr)
        : nil
}

func ipv4_IntToString(_ value: UInt32) -> String {
    let addr = in_addr(s_addr: value)
    return String(cString:inet_ntoa(addr))
}
  1. Using IPv4Address from Network
func ipv4_StringToInt(_ value: String) -> UInt32? {
    IPv4Address(value)?.rawValue.withUnsafeBytes {
        $0.load(as: UInt32.self)
    }
}

func ipv4_IntToString(_ value: UInt32) -> String {
    let data = withUnsafeBytes(of: value) { Data($0) }
    return IPv4Address(data)!.debugDescription
}

How to use:

if let addr = ipv4_StringToInt("127.0.0.1") { // addr = 0x100007F
    let ip = ipv4_IntToString(addr)
    print(ip) // Outputs: 127.0.0.1
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文