|
Steve wang |
at Feb 12, 2013 at 1:17 pm
|
⇧ |
| |
encoding/binary or strconv may help. It depends on your detailed
requirements.
// example1
import "strconv"
func Convert(data []byte) (uint32, error) {
v, err := strconv.ParseUint(string(data), 10, 32)
if err != nil {
return 0, err
}
return uint32(v), nil
}
// example2
import (
"encoding/binary"
"bytes"
)
func Convert(data []byte) (uint32, error) {
var v uint32
err := binary.Read(bytes.NewReader(data), &v)
if err != nil {
return 0, err
}
return v, nil
}
On Tuesday, February 12, 2013 8:50:43 PM UTC+8, Boris wrote:
Hi,
What is the best way to convert some bytes to an integer or other number?
For example, I take some string and md5 hash it, now I have some bytes, and
I want to convert, say, the first 4 bytes to uint32. How is best to do this?
Thanks
Boris
--
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
[email protected].
For more options, visit
https://groups.google.com/groups/opt_out.