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
+10k Golang : Edge detection with Sobel method
+11.4k Golang : Generate DSA private, public key and PEM files example
+14.7k Golang : Find commonalities in two slices or arrays example
+13k Golang : Convert(cast) uintptr to string example
+9.2k Golang : How to check if a string with spaces in between is numeric?
+26.6k Golang : How to check if a connection to database is still alive ?
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+11k How to test Facebook App on localhost ?
+23.8k Golang : Fix type interface{} has no field or no methods and type assertions example
+32.1k Golang : Convert []string to []byte examples
+10.9k Golang : Create Temporary File
+5.1k Golang : Print instead of building pyramids