Int+Extension.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // IntExtension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 12/08/14.
  6. // Copyright (C) 2014 Marcin Krzyżanowski <marcin.krzyzanowski@gmail.com>
  7. // This software is provided 'as-is', without any express or implied warranty.
  8. //
  9. // In no event will the authors be held liable for any damages arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  12. //
  13. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  14. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  15. // - This notice may not be removed or altered from any source or binary distribution.
  16. #if os(Linux) || os(Android) || os(FreeBSD)
  17. import Glibc
  18. #else
  19. import Darwin
  20. #endif
  21. /* array of bits */
  22. extension Int {
  23. init(bits: [Bit]) {
  24. self.init(bitPattern: integerFrom(bits) as UInt)
  25. }
  26. }
  27. /* array of bytes */
  28. extension Int {
  29. /** Int with collection of bytes (little-endian) */
  30. // init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
  31. // self = bytes.toInteger()
  32. // }
  33. /** Array of bytes with optional padding */
  34. func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
  35. return arrayOfBytes(value: self, length: totalBytes)
  36. }
  37. }