Search code examples
javascalaunicodeemoji

Java / Scala - Get emojis from Hexacode


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?

Solution

  • 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)
      }