Hi everybody,

I ran into this problem yesterday, I couldn't convert a string created with 
a bytes.Buffer into a big.Int by using SetString. On the other hand, with 
the same string value, created from literals or even from a byte slice, the 
conversion is successful.

Find the code below or on the playground 
at https://play.golang.org/p/Sg7ShB-okP :

package main

import (
"bytes"
"fmt"
"math/big"
)

func tryConvert(s string) {
var ok bool
bint := new(big.Int)
if bint, ok = bint.SetString(s, 10); !ok {
fmt.Println("\tFail")
} else {
fmt.Println("\tSuccess, proof: ", bint)
}
}

func main() {
buf := bytes.NewBuffer(make([]byte, 4))
buf.WriteString("1234")

fmt.Println("Conversion from bytes.Buffer.String() to big.Int:")
tryConvert(buf.String())

fmt.Println("Conversion from string to big.Int:")
tryConvert("1234")

fmt.Println("Conversion from string([]byte{}) to big.Int:")
tryConvert(string([]byte{byte('1'), byte('2'), byte('3'), byte('4')}))
}



$ go version
go version go1.9.1 linux/amd64


What am I missing?

Have a great day

Aurélien Rainone

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to