Created
January 19, 2021 08:30
-
-
Save vinhdn/21ca2b5c1b684f09e19b3d0285839b2d to your computer and use it in GitHub Desktop.
Convert string unicode to UTF-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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