Skip to content

Instantly share code, notes, and snippets.

@RockfordWei
Created September 11, 2017 20:55
Show Gist options
  • Save RockfordWei/ad63384ad7e5df3e08ebc770e393219c to your computer and use it in GitHub Desktop.
Save RockfordWei/ad63384ad7e5df3e08ebc770e393219c to your computer and use it in GitHub Desktop.
hex scan
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