Convert string to ASCII

6,784 views
Skip to first unread message

calif

unread,
Aug 29, 2010, 11:18:13 AM8/29/10
to golang-nuts
Hello!

How can I convert string (char) to ASCII code?

There is no char type.

Thanks in advance,
calif

Cory Mainwaring

unread,
Aug 29, 2010, 11:42:12 AM8/29/10
to calif, golang-nuts
var str string = "Hello"
unicodeCodePoints := []int(str)

http://golang.org/doc/go_spec.html#Conversions

ASCII doesn't exactly exist in Go. Instead, we have Unicode UTF-8, so
normal ascii converts to ascii with a []byte(str) conversion, but
unicode characters will convert to 2 slots of the byte array.
[]int(str) will get the unicode code points.

peterGo

unread,
Aug 29, 2010, 1:20:05 PM8/29/10
to golang-nuts
Cory,

> unicode characters will convert to 2 slots of the byte array.

UTF-8 uses one to four bytes to express a Unicode code point
(utf8.UTFMax = 4 ).

http://golang.org/pkg/utf8/#Constants

What is the definition of UTF-8?
http://unicode.org/faq/utf_bom.html#utf8-1

Peter

calif

unread,
Aug 29, 2010, 5:25:35 PM8/29/10
to golang-nuts
Thank you very much!

It works!

:)
Reply all
Reply to author
Forward
0 new messages