How to convert the elements of strings to uint

1,354 views
Skip to first unread message

qubicllj

unread,
Sep 1, 2010, 12:07:37 PM9/1/10
to golang-nuts
How to convert the elements of strings to uint

Evan Shaw

unread,
Sep 1, 2010, 12:12:08 PM9/1/10
to qubicllj, golang-nuts
On Wed, Sep 1, 2010 at 11:07 AM, qubicllj <qubi...@gmail.com> wrote:
> How to convert the elements of strings to uint

Are you asking how to convert the string "123" to the uint 123? If so,
look at the strconv package; specifically Atoui.

Are you asking how to get the Unicode code points of the string? If so, try

points := []int(theString)

- Evan

jimmy frasche

unread,
Sep 1, 2010, 12:12:19 PM9/1/10
to qubicllj, golang-nuts

qubicllj

unread,
Sep 1, 2010, 7:49:25 PM9/1/10
to golang-nuts
I am asking how to convert the string "123" to int array [1 2 3],the
number string may be too large to convert to uint directly,so i want
to convert the number string to number array

On Sep 2, 12:12 am, Evan Shaw <chicken...@gmail.com> wrote:

fango

unread,
Sep 1, 2010, 8:15:31 PM9/1/10
to golang-nuts
Good question, as it brings up :

package main

package main

func main(){
s := "123ä"
a := []byte(s)
b := []int(s)
for i, v := range s {
println(v, a[i], b[i] )
}
println(len(s), len(a), len(b))
}

Output:
49 49 49
50 50 50
51 51 51
228 195 228
5 5 4

String is UTF-8, so ASCII (<128) is single byte, thus can directly
convert to byte or int. ( - '0' if you want the decimal, not the ascii
value).

Further on, Range does one more favor for you. It converts UTF-8 byte
sequence to unicode int. Thus one less in []int len for the two byte ä

From spec: `For a value of a string type, the "range" clause iterates
over the Unicode code points in the string. On successive iterations,
the index variable will be the index of the first byte of successive
UTF-8-encoded code points in the string, and the second variable, of
type int, will be the value of the corresponding code point. `

Cheers,
Fango

peterGo

unread,
Sep 1, 2010, 8:18:13 PM9/1/10
to golang-nuts
qubicllj,

The underlying type for UTF-8 encoded strings is byte.

package main

import (
"fmt"
)

func nstoi(s string) []int {
i := make([]int, len(s))
for n := 0; n < len(s); n++ {
i[n] = int(s[n]) - '0'
}
return i
}

func main() {
s := "123"
n := nstoi(s)
fmt.Println(s, n)
}

Peter

Skip Tavakkolian

unread,
Sep 2, 2010, 1:29:12 AM9/2/10
to qubicllj, golang-nuts
package main

import (
"fmt"
)
func main() {
s := "2½4¼"
x := make([]int, len(s))
n := 0
for _,v := range s {
x[n] = int(v) - '0'
n++
}

fmt.Println(s, x)

fango

unread,
Sep 2, 2010, 2:30:17 AM9/2/10
to golang-nuts
Thanks for pointing out: "index variable will be the index of the
first byte", not character, so that "2½4¼" will panic my code.

Regards,
Fango

On Sep 2, 1:29 pm, Skip Tavakkolian <skip.tavakkol...@gmail.com>
wrote:

fango

unread,
Sep 2, 2010, 2:38:29 AM9/2/10
to golang-nuts
Besides, refrain from using print and println, in spec it says they'll
disappear.
So in a word it is a good exercise to understand string :)

Cheers,
Fango

chris dollin

unread,
Sep 2, 2010, 3:21:56 AM9/2/10
to fango, golang-nuts
On 2 September 2010 07:38, fango <fan.h...@gmail.com> wrote:
> Besides, refrain from using print and println, in spec it says they'll
> disappear.
> So in a word it is a good exercise to understand string :)

Just to be clear: it's the built-ins print & println that might disappear,
not fmt.Print &co.

(It's still good to understand strings.)

Chris

--
Chris "allusive" Dollin

Reply all
Reply to author
Forward
0 new messages