Created
September 11, 2017 20:55
-
-
Save RockfordWei/ad63384ad7e5df3e08ebc770e393219c to your computer and use it in GitHub Desktop.
hex scan
This file contains 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
import Foundation | |
public extension String { | |
public func scanHex() -> UInt32? { | |
var value = UInt32(0) | |
#if os(Linux) | |
if Scanner(string: self).scanHexInt(&value) { | |
return value | |
} else { | |
return nil | |
} | |
#else | |
if Scanner(string: self).scanHexInt32(&value) { | |
return value | |
} else { | |
return nil | |
} | |
#endif | |
} | |
} | |
if let x = "0x100".scanHex() { | |
print("scanned:", x) | |
} else { | |
print("scan failed") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment