Golang : convert rune to integer value
Just a short tutorial on how to convert a rune into integer. Converting rune to integer value can be useful for generating checksum to ensure that the message transmitted is not tempered with.
package main
import "fmt"
func main() {
r1 := rune('你')
i1 := int(r1)
fmt.Println(i1)
fmt.Println(string(i1))
r2 := rune('好')
i2 := int(r2)
fmt.Println(i2)
fmt.Println(string(i2))
r3 := rune('吗')
i3 := int(r3)
fmt.Println(i3)
fmt.Println(string(i3))
// or in a more efficient way
message := []rune{'你', '好', '吗'}
fmt.Println(message)
}
http://play.golang.org/p/J1WHRo4Qtk
Output :
20320
你
22909
好
21527
吗
[20320 22909 21527]
See also : Golang : convert rune to unicode hexadecimal value and back to rune character
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
+15k Golang : How do I get the local IP (non-loopback) address ?
+36.2k Golang : How to split or chunking a file to smaller pieces?
+7.7k Golang : Example of how to detect which type of script a word belongs to
+18.6k Golang : convert int to string
+5.8k Golang : Detect variable or constant type
+7.1k Golang : Use modern ciphers only in secure connection
+6.5k Unix/Linux : How to get own IP address ?
+10.4k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+29.3k Golang : How to create new XML file ?
+5.6k Fix fatal error: evacuation not done in time problem
+13.4k Golang : Count number of runes in string
+7.3k Golang : Create zip/ePub file without compression(use Store algorithm)