Created
March 18, 2023 23:57
-
-
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.
This file contains 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
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