Skip to content

Instantly share code, notes, and snippets.

@vinhdn
Created January 19, 2021 08:30
Show Gist options
  • Save vinhdn/21ca2b5c1b684f09e19b3d0285839b2d to your computer and use it in GitHub Desktop.
Save vinhdn/21ca2b5c1b684f09e19b3d0285839b2d to your computer and use it in GitHub Desktop.
Convert string unicode to UTF-8
import java.util.regex.Matcher
import java.util.regex.Pattern
fun String.unicode(): String {
return unicodeToUtf8().unicodeToUtf8(16)
}
fun String.unicodeToUtf8(radix: Int = 10): String {
val p = Pattern.compile(if (radix == 10) "&#(\\d+);|." else "&#x(.+?);|.")
val sb = StringBuilder()
val m: Matcher = p.matcher(this)
while (m.find()) if (m.group(1) != null) {
sb.append(m.group(1).toInt(radix).toChar())
} else {
sb.append(m.group(0) as String)
}
return sb.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment