在PURE Swift中将十六进制字符串转换为字符 [英] Hex String to Character in PURE Swift
问题描述
我需要一种方法来将包含表示十六进制值的字符串的字符串转换为与该特定十六进制值对应的字符。
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.
理想情况下, lines:
Ideally, something along these lines:
let hexString: String = "2C"
let char: Character = fromHexString(hexString)
println(char) // prints -> ","
我尝试使用语法:\u {n} n是一个Int或String,既不工作。
I've tried to use the syntax: "\u{n}" where n is a Int or String and neither worked.
这可以用于循环一个如下的hexStrings数组:
This could be used to loop over an array of hexStrings like so:
var hexArray = ["2F", "24", "40", "2A"]
var charArray = [Character]()
charArray = map(hexArray) { charArray.append(Character($0)) }
charArray.description // prints -> "[/, $, @, *]"
推荐答案
有关您的代码的几个方面:
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)) }
您可以使用 hexArray.map
而不是 map(hexArray)
,当你使用map函数时,你在概念上做的是映射接收器的元素数组到一组新的值,映射的结果是新的mapped数组,这意味着你不需要在地图中做 charArray.append
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.
无论如何,这里是一个工作示例:
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) // -> [/, $, @, *]
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另一个选项:
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)
}
最终编辑:解释
由于ascii表中包含所有数字012345679),我们可以将'N'(基数10)转换为知道ascii值为0的整数。
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.
因为:
'0': 48
'1': 49
...
'9': 57
然后,如果你需要将'9'转换为9,你可以做
Then if for example you need to convert '9' to 9 you could do
asciiValue('9') - asciiValue('0') => 57 - 48 = 9
您可以从'A'到'F' / p>
And you can do the same from 'A' to 'F':
'A': 65
'B': 66
...
'F': 70
现在我们可以像以前一样做, d do:
Now we can do the same as before but, for example for 'F' we'd do:
asciiValue('F') - asciiValue('A') => 70 - 65 = 5
请注意,我们需要将10加到此数字以获得小数。然后(回到代码):如果标量在AZ之间,我们需要做:
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 + asciiValue(<letter>) - asciiValue('A')
> 10 + scalar.value - 65
which is the same as: 10 + scalar.value - 65
如果它在0-9之间:
asciiValue(<letter>) - asciiValue('0')
这与相同: scalar.value - 48
'2'为2,'F'为15(按上例),右?因为hex是16,我们需要做:
'2' is 2 and 'F' is 15 (by the previous example), right?. Since hex is base 16 we'd need to do:
((16 ^ 1)* 2)+((16 ^ 0)* 15)= 47
这篇关于在PURE Swift中将十六进制字符串转换为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!