I need a way to convert a string that contains a literal string representing a hexadecimal value into a Character corresponding to that particular hexadecimal value.
我需要一种方法将包含表示十六进制值的文字字符串的字符串转换为对应于该特定十六进制值的字符。
Ideally, something along these lines:
理想情况下,这些方面的内容:
let hexString: String = "2C"
let char: Character = fromHexString(hexString)
println(char) // prints -> ","
I've tried to use the syntax: "\u{n}" where n is a Int or String and neither worked.
我试过使用语法:“\ u {n}”其中n是Int或String,但都不起作用。
This could be used to loop over an array of hexStrings like so:
这可以用来循环遍历一个hexStrings数组,如下所示:
var hexArray = ["2F", "24", "40", "2A"]
var charArray = [Character]()
charArray = map(hexArray) { charArray.append(Character($0)) }
charArray.description // prints -> "[/, $, @, *]"
2 个解决方案
#1
10
A couple of things about your code:
关于代码的一些事情:
var charArray = [Character]()
charArray = map(hexArray) { charArray.append(Character($0)) }
You don't need to create an array and then assign the result of the map, you can just assign the result and avoid creating an unnecessary array.
您不需要创建数组然后分配地图的结果,您只需分配结果并避免创建不必要的数组。
charArray = map(hexArray) { charArray.append(Character($0)) }
Here you can use hexArray.map
instead of map(hexArray)
, also when you use a map function what you are conceptually doing is mapping the elements of the receiver array to a new set of values and the result of the mapping is the new "mapped" array, which means that you don't need to do charArray.append
inside the map closure.
在这里你可以使用hexArray.map而不是map(hexArray),当你使用map函数时,你在概念上正在做的是将接收器数组的元素映射到一组新的值,并且映射的结果是新的“映射“数组,这意味着你不需要在map闭包内做charArray.append。
Anyway, here is a working example:
无论如何,这是一个工作的例子:
let hexArray = ["2F", "24", "40", "2A"]
var charArray = hexArray.map { char -> Character in
let code = Int(strtoul(char, nil, 16))
return Character(UnicodeScalar(code))
}
println(charArray) // -> [/, $, @, *]
EDIT: This is another implementation that doesn't need Foundation:
编辑:这是另一个不需要基金会的实现:
func hexToScalar(char: String) -> UnicodeScalar {
var total = 0
for scalar in char.uppercaseString.unicodeScalars {
if !(scalar >= "A" && scalar <= "F" || scalar >= "0" && scalar <= "9") {
assertionFailure("Input is wrong")
}
if scalar >= "A" {
total = 16 * total + 10 + scalar.value - 65 /* 'A' */
} else {
total = 16 * total + scalar.value - 48 /* '0' */
}
}
return UnicodeScalar(total)
}
let hexArray = ["2F", "24", "40", "2A"]
var charArray = hexArray.map { Character(hexToScalar($0)) }
println(charArray)
EDIT2 Yet another option:
EDIT2又一个选择:
func hexToScalar(char: String) -> UnicodeScalar {
let map = [ "0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15 ]
let total = reduce(char.uppercaseString.unicodeScalars, 0, { $0 * 16 + (map[String($1)] ?? 0xff) })
if total > 0xFF {
assertionFailure("Input char was wrong")
}
return UnicodeScalar(total)
}
Final edit: explanation
最终编辑:解释
Given that the ascii table has all the number together (012345679), we can convert 'N' (base 10) to an integer knowing the ascii value of 0.
鉴于ascii表具有所有数字(012345679),我们可以将'N'(基数10)转换为知道ascii值为0的整数。
Because:
'0': 48
'1': 49
...
'9': 57
Then if for example you need to convert '9' to 9 you could do
然后,如果您需要将'9'转换为9,则可以执行此操作
asciiValue('9') - asciiValue('0') => 57 - 48 = 9
And you can do the same from 'A' to 'F':
你可以从“A”到“F”做同样的事情:
'A': 65
'B': 66
...
'F': 70
Now we can do the same as before but, for example for 'F' we'd do:
现在我们可以像以前一样做,但是,例如'F',我们做:
asciiValue('F') - asciiValue('A') => 70 - 65 = 5
Note that we need to add 10 to this number to get the decimal. Then (going back to the code): If the scalar is between A-Z we need to do:
请注意,我们需要在此数字上加10才能获得小数。然后(回到代码):如果标量在A-Z之间,我们需要做:
10 + asciiValue(<letter>) - asciiValue('A')
which is the same as: 10 + scalar.value - 65
这与:10 + scalar.value - 65相同
And if it's between 0-9:
如果它在0-9之间:
asciiValue(<letter>) - asciiValue('0')
which is the same as: scalar.value - 48
这与:scalar.value - 48相同
For example: '2F'
'2' is 2 and 'F' is 15 (by the previous example), right?. Since hex is base 16 we'd need to do:
'2'是2,'F'是15(前面的例子),对吗?由于hex是16,我们需要做:
((16 ^ 1) * 2) + ((16 ^ 0) * 15) = 47
((16 ^ 1)* 2)+((16 ^ 0)* 15)= 47
#2
4
Here you go:
干得好:
var string = String(UnicodeScalar(Int("2C", radix: 16)!))
BTW, you can include hex values in the literal strings like this:
顺便说一句,您可以在文字字符串中包含十六进制值,如下所示:
var string = "\u{2c}"
#1
10
A couple of things about your code:
关于代码的一些事情:
var charArray = [Character]()
charArray = map(hexArray) { charArray.append(Character($0)) }
You don't need to create an array and then assign the result of the map, you can just assign the result and avoid creating an unnecessary array.
您不需要创建数组然后分配地图的结果,您只需分配结果并避免创建不必要的数组。
charArray = map(hexArray) { charArray.append(Character($0)) }
Here you can use hexArray.map
instead of map(hexArray)
, also when you use a map function what you are conceptually doing is mapping the elements of the receiver array to a new set of values and the result of the mapping is the new "mapped" array, which means that you don't need to do charArray.append
inside the map closure.
在这里你可以使用hexArray.map而不是map(hexArray),当你使用map函数时,你在概念上正在做的是将接收器数组的元素映射到一组新的值,并且映射的结果是新的“映射“数组,这意味着你不需要在map闭包内做charArray.append。
Anyway, here is a working example:
无论如何,这是一个工作的例子:
let hexArray = ["2F", "24", "40", "2A"]
var charArray = hexArray.map { char -> Character in
let code = Int(strtoul(char, nil, 16))
return Character(UnicodeScalar(code))
}
println(charArray) // -> [/, $, @, *]
EDIT: This is another implementation that doesn't need Foundation:
编辑:这是另一个不需要基金会的实现:
func hexToScalar(char: String) -> UnicodeScalar {
var total = 0
for scalar in char.uppercaseString.unicodeScalars {
if !(scalar >= "A" && scalar <= "F" || scalar >= "0" && scalar <= "9") {
assertionFailure("Input is wrong")
}
if scalar >= "A" {
total = 16 * total + 10 + scalar.value - 65 /* 'A' */
} else {
total = 16 * total + scalar.value - 48 /* '0' */
}
}
return UnicodeScalar(total)
}
let hexArray = ["2F", "24", "40", "2A"]
var charArray = hexArray.map { Character(hexToScalar($0)) }
println(charArray)
EDIT2 Yet another option:
EDIT2又一个选择:
func hexToScalar(char: String) -> UnicodeScalar {
let map = [ "0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15 ]
let total = reduce(char.uppercaseString.unicodeScalars, 0, { $0 * 16 + (map[String($1)] ?? 0xff) })
if total > 0xFF {
assertionFailure("Input char was wrong")
}
return UnicodeScalar(total)
}
Final edit: explanation
最终编辑:解释
Given that the ascii table has all the number together (012345679), we can convert 'N' (base 10) to an integer knowing the ascii value of 0.
鉴于ascii表具有所有数字(012345679),我们可以将'N'(基数10)转换为知道ascii值为0的整数。
Because:
'0': 48
'1': 49
...
'9': 57
Then if for example you need to convert '9' to 9 you could do
然后,如果您需要将'9'转换为9,则可以执行此操作
asciiValue('9') - asciiValue('0') => 57 - 48 = 9
And you can do the same from 'A' to 'F':
你可以从“A”到“F”做同样的事情:
'A': 65
'B': 66
...
'F': 70
Now we can do the same as before but, for example for 'F' we'd do:
现在我们可以像以前一样做,但是,例如'F',我们做:
asciiValue('F') - asciiValue('A') => 70 - 65 = 5
Note that we need to add 10 to this number to get the decimal. Then (going back to the code): If the scalar is between A-Z we need to do:
请注意,我们需要在此数字上加10才能获得小数。然后(回到代码):如果标量在A-Z之间,我们需要做:
10 + asciiValue(<letter>) - asciiValue('A')
which is the same as: 10 + scalar.value - 65
这与:10 + scalar.value - 65相同
And if it's between 0-9:
如果它在0-9之间:
asciiValue(<letter>) - asciiValue('0')
which is the same as: scalar.value - 48
这与:scalar.value - 48相同
For example: '2F'
'2' is 2 and 'F' is 15 (by the previous example), right?. Since hex is base 16 we'd need to do:
'2'是2,'F'是15(前面的例子),对吗?由于hex是16,我们需要做:
((16 ^ 1) * 2) + ((16 ^ 0) * 15) = 47
((16 ^ 1)* 2)+((16 ^ 0)* 15)= 47
#2
4
Here you go:
干得好:
var string = String(UnicodeScalar(Int("2C", radix: 16)!))
BTW, you can include hex values in the literal strings like this:
顺便说一句,您可以在文字字符串中包含十六进制值,如下所示:
var string = "\u{2c}"