Int+Extension.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // IntExtension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 12/08/14.
  6. // Copyright (C) 2014-2017 Krzyżanowski <marcin@krzyzanowskim.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. //
  17. #if os(Linux) || os(Android) || os(FreeBSD)
  18. import Glibc
  19. #else
  20. import Darwin
  21. #endif
  22. /* array of bits */
  23. extension Int {
  24. init(bits: [Bit]) {
  25. self.init(bitPattern: integerFrom(bits) as UInt)
  26. }
  27. }
  28. /* array of bytes */
  29. extension Int {
  30. /** Array of bytes with optional padding */
  31. func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
  32. return arrayOfBytes(value: self, length: totalBytes)
  33. }
  34. }