Golang : Convert a rune to unicode style string \u
Continuing from previous tutorial on how to get escape characters or \u unicode style string. These are some additional methods to convert rune to unicode style string.
package main
import (
"fmt"
)
func main() {
// use %x for single rune
fmt.Printf("\\u%x\n", 'お')
// fmt.Printf("\\u%x\n", 'おはよう') -- will not work
// use %+q for multiple runes
fmt.Printf("%+q\n", "おはよう")
}
output :
\u304a
"\u304a\u306f\u3088\u3046"
Another way :
package main
import (
"fmt"
)
func main() {
u := fmt.Sprintf("%U", 'お')
fmt.Println(u)
}
Output :
U+304A
or use strconv.QuoteRuneToASCII()
function.
package main
import (
"fmt"
"strconv"
)
func main() {
// translate character to rune
r := rune('お')
// extra the unicode \uxxxx value
unicode := strconv.QuoteRuneToASCII(r)
fmt.Println(string(r)+" unicode string value is : ", unicode)
}
Output :
お unicode string value is : '\u304a'
Reference :
See also : Golang : Get escape characters \u form from unicode characters
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.6k PHP : Get coordinates latitude/longitude from string
+9.3k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+8.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+10.5k Golang : ISO8601 Duration Parser example
+15.3k Golang : ROT47 (Caesar cipher by 47 characters) example
+5.2k Javascript : Change page title to get viewer attention
+4.7k Javascript : How to get width and height of a div?
+14.2k Golang : How to shuffle elements in array or slice?
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+11k Golang : Web routing/multiplex example
+8.6k Golang : Executing and evaluating nested loop in html template
+18.3k Golang : Example for RSA package functions