简体   繁体   English

如何在Swift中将Unicode字符转换为Int

[英]How to convert Unicode Character to Int in Swift

A user asked the following question to one of my answers . 用户向 我的一个答案 询问了以下问题。

I have a unicode character \\u{0D85} . 我有一个unicode字符\\u{0D85} How do I get the Int value from it? 如何从中获取Int值?

I was going to refer them to another Stack Overflow Q&A but I couldn't find one. 我打算将它们引用到另一个Stack Overflow Q&A,但我找不到一个。 These refer to converting the other direction: 这些是指转换另一个方向:

And these seem to be asking how to convert a number in string form to an actual Int (as in converting "1" to 1 ). 这些似乎是在询问如何将字符串形式的数字转换为实际的Int (如将"1"转换为1 )。

Rather than try to fit my answer in a comment to the asker, I am going to provide an answer below. 我将在下面提供一个答案,而不是试图将我的答案放在对提问者的评论中。 The Type of \\u{0D85} is somewhat unclear but I will cover the various possibilities. \\u{0D85}的类型有点不清楚,但我会介绍各种可能性。

Hex to Int Hex到Int

If you are starting with \\u{0D85} and you know the hex value of the Unicode character, then you might as well write it in the following format because it is an Int already. 如果您从\\u{0D85}并且您知道Unicode字符的十六进制值,那么您也可以使用以下格式编写它,因为它已经是Int

let myInt = 0x0D85                          // Int: 3461

String to Int 字符串到Int

I assume, though, that you have "\\u{0D85}" (in quotes), which makes it a String by default. 不过,我假设您有"\\u{0D85}" (引号),默认情况下它是一个String And since it is a String , you can't be certain that you will only have a single Int value for the general case. 由于它是一个String ,因此您无法确定一般情况下只有一个Int值。

let myString = "\u{0D85}"

for element in myString.unicodeScalars {
    let myInt = element.value               // UInt32: 3461
}

I could have also used myString.utf16 and let myInt = Int(element) , but I find it easier to deal with Unicode scalar values (UTF-32) when there is a possibility of things like emoji. 我也可以使用myString.utf16let myInt = Int(element) ,但是当有可能出现像表情符号这样的东西时,我发现更容易处理Unicode标量值(UTF-32)。 (See this answer for more details.) (有关详细信息,请参阅此答案 。)

Character to Int Int的字符

Swift Character , which is an extended grapheme cluster , does not have a utf16 or unicodeScalars property, so if you are starting with Character then convert it to a String first and then follow the directions in the String to Int section above. Swift Character是一个扩展的字形集群 ,没有utf16unicodeScalars属性,因此如果您从Character开始,则首先将其转换为String ,然后按照上面String to Int部分中的说明进行操作。

let myChar: Character = "\u{0D85}"
let myString = String(myChar)

With Swift 5, Unicode.Scalar has a value property. 使用Swift 5, Unicode.Scalar具有value属性。 value has the following declaration: value具有以下声明:

A numeric representation of the Unicode scalar. Unicode标量的数字表示。

var value: UInt32 { get }

The following Playground sample codes show how to iterate over the unicodeScalars property of a Character or a String and print the Int32 value of each Unicode scalar: 以下Playground示例代码显示如何迭代CharacterStringunicodeScalars属性并打印每个Unicode标量的Int32值:

let char: Character = "\u{0D85}"

for scalar in char.unicodeScalars {
    print(scalar.value)
}

/*
 prints: 3461
 */
let str: String = "\u{0D85}"

for scalar in str.unicodeScalars {
    print(scalar.value)
}

/*
 prints: 3461
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM