-
-
Save vukcevich/82762bc6ead931c35900601f7aa98ccd to your computer and use it in GitHub Desktop.
Convert an arbitrary length byte array into a Swift Int
This file contains hidden or 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
| /** | |
| IntExtensions.swift | |
| Convert an arbitrary length byte array into a Swift Int | |
| <https://gist.github.com/tannernelson/e720877bf7700138eb99> | |
| */ | |
| extension Int { | |
| static func fromByteArray(bytes: [UInt8]) -> Int { | |
| var int = 0 | |
| for (offset, byte) in enumerate(bytes) { | |
| let factor: Double = Double(bytes.count) - (Double(offset) + 1); | |
| let size: Double = 256 | |
| int += Int(byte) * Int(pow(size, factor)) | |
| } | |
| return int | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment