Java / Scala – 从十六进制代码获取表情符号

huangapple go评论52阅读模式
英文:

Java / Scala - Get emojis from Hexacode

问题

如果我有一个表情符号的十六进制代码,如何从中生成完整的表情符号。如果它是一个单一的十六进制代码,我可以生成如下:

val str1 = "1F471"
val hex = Integer.parseInt(str1, 16)
println(new String(Character.toChars(hex)))

这会打印出 👱。然而,如果我想要打印出由第二个组件即 1F3FF 给出的带肤色的完整表情符号,需要做什么?

val str = "1F471 1F3FF"
val parts = str.split(" ").map { Integer.parseInt(it, 16) }
println(mergeEmojis(new String(Character.toChars(parts[0])), new String(Character.toChars(parts[1]))))
// 如何实现 mergeEmojis()?

如何实现 mergeEmojis()

英文:

Suppose I have a hexa code for an emoji, how can I generate the full emoji from the same. If its a single hexacode I am able to generate is as follows:

val str1 = "1F471"
val hex = Integer.parseInt(str1, 16)
println(new String(Character.toChars(hex)))

his prints 👱. However, if I want to print the full emoji with skin tone given by the second component i.e 1F3FF, what needs to be done?

val str = "1F471 1F3FF"
val parts = str.split(" ").map(x => Integer.parseInt(x, 16))
println(mergeEmojis(new String(Character.toChars(parts(0))), new String(Character.toChars(parts(1))))) 
// how can mergeEmojis() be implemented?

答案1

得分: 0

根据 @Ackdari 的建议,连接字符可以起作用

// 将表情代码数组转换为表情字符串的方法
private def toEmoji(hexCodes: Array[Int]): String = {
var emojiCharsCombined: Array[Char] = Array.emptyCharArray
hexCodes.foreach(emojiComponent => {
val emojiChars: Array[Char] = Character.toChars(emojiComponent)
emojiCharsCombined = concat(emojiCharsCombined, emojiChars)
})
new String(emojiCharsCombined)
}

英文:

As suggested by @Ackdari, concatenating the characters works

  // method to convert array of emoji codes to emoji string
  private def toEmoji(hexCodes: Array[Int]): String = {
    var emojiCharsCombined: Array[Char] = Array.emptyCharArray
    hexCodes.foreach(emojiComponent => {
      val emojiChars: Array[Char] = Character.toChars(emojiComponent)
      emojiCharsCombined = concat(emojiCharsCombined, emojiChars)
    })
    new String(emojiCharsCombined)
  }

huangapple
  • 本文由 发表于 2020年7月22日 16:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63030209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定