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
+11.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+8k Golang : Go as a script or running go with shebang/hashbang style
+5.9k Golang : constant 20013 overflows byte error message
+9.5k Golang : Create matrix with Gonum Matrix package example
+4.9k Golang : How to search a list of records or data structures
+29.2k Golang : How to redirect to new page with net/http?
+13k Golang : Send email with attachment(RFC2822) using Gmail API example
+32k Golang : Converting a negative number to positive number
+5.1k Golang : Get missing location after unmarshal binary and gob decode time.
+26.6k Golang : Change a file last modified date and time
+5.5k Golang : How to validate ISBN?
+4.8k PHP : Get client IP address