Skip to content

Instantly share code, notes, and snippets.

@alyssais
Last active January 11, 2024 15:04
Show Gist options
  • Save alyssais/654f585b6020e5655132 to your computer and use it in GitHub Desktop.
Save alyssais/654f585b6020e5655132 to your computer and use it in GitHub Desktop.
import Darwin
let isLittleEndian = Int(OSHostByteOrder()) == OSLittleEndian
let htons = isLittleEndian ? _OSSwapInt16 : { $0 }
let htonl = isLittleEndian ? _OSSwapInt32 : { $0 }
let htonll = isLittleEndian ? _OSSwapInt64 : { $0 }
let ntohs = isLittleEndian ? _OSSwapInt16 : { $0 }
let ntohl = isLittleEndian ? _OSSwapInt32 : { $0 }
let ntohll = isLittleEndian ? _OSSwapInt64 : { $0 }
@robert-v
Copy link

This was super helpful when searching for ntohs equivalent in Swift. Thank you! Robert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment