Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created March 18, 2023 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlynorama/f5c01b99221269f9495dee61321f4980 to your computer and use it in GitHub Desktop.
Save carlynorama/f5c01b99221269f9495dee61321f4980 to your computer and use it in GitHub Desktop.
Converting a fixed width integer to Data in Swift. e.g. UInt32/Int32, etc.
extension FixedWidthInteger {
var dataBigEndian: Data {
var int = self.bigEndian
return Data(bytes: &int, count: MemoryLayout<Self>.size)
}
var dataLittleEndian: Data {
var int = self.littleEndian
return Data(bytes: &int, count: MemoryLayout<Self>.size)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment