UInt16+Extension.swift 775 B

12345678910111213141516171819202122232425
  1. //
  2. // UInt16+Extension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 06/08/16.
  6. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. /** array of bytes */
  9. extension UInt16 {
  10. init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
  11. self = UInt16(bytes: bytes, fromIndex: bytes.startIndex)
  12. }
  13. init<T: Collection>(bytes: T, fromIndex index: T.Index) where T.Iterator.Element == UInt8, T.Index == Int {
  14. let val0 = UInt16(bytes[index.advanced(by: 0)]) << 8
  15. let val1 = UInt16(bytes[index.advanced(by: 1)])
  16. self = val0 | val1
  17. }
  18. func bytes(totalBytes: Int = MemoryLayout<UInt16>.size) -> Array<UInt8> {
  19. return arrayOfBytes(value: self, length: totalBytes)
  20. }
  21. }