FAQ
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.

Search Discussions

  • Nate Finch at Feb 12, 2013 at 1:07 pm
    Bit shifting, my friend:

    http://play.golang.org/p/3Nt78A6nk9

    Not sure if there's some magic way to do it in Go via casting, but this
    should do the same thing.

    it takes the bytes from the array and uses them as the bytes for the
    uint32, with the most significant bits first.

    So, if your bytes were:

    0000 0000 0001 0000

    The integer would turn into 256.


    On Tuesday, February 12, 2013 7:50:43 AM UTC-5, 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.
  • Dmitry Chestnykh at Feb 12, 2013 at 1:09 pm
    See http://golang.org/pkg/encoding/binary
    On Tuesday, February 12, 2013 1:50:43 PM UTC+1, 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.
  • Nate Finch at Feb 12, 2013 at 1:11 pm
    Bah, magic. Figures.
    On Tuesday, February 12, 2013 8:08:58 AM UTC-5, Dmitry Chestnykh wrote:

    See http://golang.org/pkg/encoding/binary
    On Tuesday, February 12, 2013 1:50:43 PM UTC+1, 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.
  • Nate Finch at Feb 12, 2013 at 1:09 pm
    Byte shifting, my friend.

    http://play.golang.org/p/3Nt78A6nk9

    So if your bytes were

    00000000 00000000 00000001 00000000

    Then the integer value would be 256.

    Not sure if there's a magic way to do it via casting, but this should work.
    On Tuesday, February 12, 2013 7:50:43 AM UTC-5, 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.
  • John Asmuth at Feb 12, 2013 at 1:58 pm
    If you want to do your own byte shifting, make sure to read
    http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
    On Tuesday, February 12, 2013 8:09:51 AM UTC-5, Nate Finch wrote:

    Byte shifting, my friend.

    http://play.golang.org/p/3Nt78A6nk9

    So if your bytes were

    00000000 00000000 00000001 00000000

    Then the integer value would be 256.

    Not sure if there's a magic way to do it via casting, but this should work.
    On Tuesday, February 12, 2013 7:50:43 AM UTC-5, 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.
  • Dan Kortschak at Feb 12, 2013 at 7:39 pm
    That's more significant if you do it this way: u32 = *(*uint32)(unsafe.Pointer(&b[0])). Which you shouldn't do for some of the reasons in that fine article - and many others.

    Dan

    On 13/02/2013, at 12:28 AM, "John Asmuth" wrote:

    If you want to do your own byte shifting, make sure to read http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

    --
    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.
  • Michael Jones at Feb 12, 2013 at 8:13 pm
    of course you could just be clear in the code...

    v := int32(b[0]) << 24 | int32(b[1]) << 16 | int32(b[2]) << 8 | int32(b3)

    ...which a good compiler will turn into a 32-bit clear and four single
    byte moves, something that can be done almost a billion times per
    second.

    On Tue, Feb 12, 2013 at 11:39 AM, Dan Kortschak
    wrote:
    That's more significant if you do it this way: u32 =
    *(*uint32)(unsafe.Pointer(&b[0])). Which you shouldn't do for some of the
    reasons in that fine article - and many others.

    Dan

    On 13/02/2013, at 12:28 AM, "John Asmuth" wrote:

    If you want to do your own byte shifting, make sure to read
    http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

    --
    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.


    --
    Michael T. Jones | Chief Technology Advocate | [email protected] | +1
    650-335-5765

    --
    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.
  • Dan Kortschak at Feb 12, 2013 at 8:38 pm
    Exactly.
    On 13/02/2013, at 6:43 AM, "Michael Jones" wrote:

    of course you could just be clear in the code...

    v := int32(b[0]) << 24 | int32(b[1]) << 16 | int32(b[2]) << 8 | int32(b3)

    ...which a good compiler will turn into a 32-bit clear and four single
    byte moves, something that can be done almost a billion times per
    second.
    --
    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.
  • Shane Hansen at Feb 14, 2013 at 7:48 am
    You should just use the bignum package to convert to int64.
    http://play.golang.org/p/gKURNosdmm
    On Tue, Feb 12, 2013 at 1:38 PM, Dan Kortschak wrote:

    Exactly.
    On 13/02/2013, at 6:43 AM, "Michael Jones" wrote:

    of course you could just be clear in the code...

    v := int32(b[0]) << 24 | int32(b[1]) << 16 | int32(b[2]) << 8 | int32(b3)

    ...which a good compiler will turn into a 32-bit clear and four single
    byte moves, something that can be done almost a billion times per
    second.
    --
    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.

    --
    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.
  • 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 12, '13 at 12:57p
activeFeb 14, '13 at 7:48a
posts11
users8
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase