标签:let hex cells 开始 inline 十六 ref print ber
|
1
2
3
4
|
//将文本框中的值转换成数字var i = (tf1.text as NSString).intValuevar f = (tf1.text as NSString).floatValuevar d = (tf1.text as NSString).doubleValue |
|
1
2
3
4
5
6
7
8
9
10
11
|
func hexStringToInt(from:String) -> Int { let str = from.uppercased() var sum = 0 for i in str.utf8 { sum = sum * 16 + Int(i) - 48 // 0-9 从48开始 if i >= 65 { // A-Z 从65开始,但有初始值10,所以应该是减去55 sum -= 7 } } return sum} |
|
1
2
3
|
let str = "FF0000"let value = hexStringToInt(from:str)print(value) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
extension String{ func hexStringToInt() -> Int { let str = self.uppercased() var sum = 0 for i in str.utf8 { sum = sum * 16 + Int(i) - 48 // 0-9 从48开始 if i >= 65 { // A-Z 从65开始,但有初始值10,所以应该是减去55 sum -= 7 } } return sum }} |
|
1
2
3
|
let str = "FF0000"let value = str.hexStringToInt()print(value) |
Swift - 将String类型的数字转换成数字类型(支持十进制、十六进制)
标签:let hex cells 开始 inline 十六 ref print ber
原文地址:http://www.cnblogs.com/Free-Thinker/p/7243683.html