Int.swift 527 B

12345678910111213141516171819202122232425
  1. //
  2. // Int.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 12/26/15.
  6. // Copyright © 2015 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. extension Int {
  10. init<T: Collection>(bigEndianBytes bytes: T) where T.Element == UInt8 {
  11. assert(bytes.count <= 4)
  12. var result: UInt = 0
  13. for (index, byte) in bytes.enumerated() {
  14. let shiftAmount = UInt((bytes.count) - index - 1) * 8
  15. result += UInt(byte) << shiftAmount
  16. }
  17. self.init(result)
  18. }
  19. }