dongsui4658 2014-07-11 22:01
浏览 47
已采纳

为什么这两个golang整数转换函数给出不同的结果?

I wrote a function to convert a byte slice to an integer.

The function I created is actually a loop-based implemtation of what Rob Pike published here:

http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

Here is Rob's code:

i = (data[0]<<0) | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);

My first implementation (toInt2 in the playground) doesn't work as I expected because it appears to initialize the int value as a uint. This seems really strange but it must be platform specific because the go playground reports a different result than my machine (a mac).

Can anyone explain why these functions behave differently on my mac?

Here's the link to the playground with the code: http://play.golang.org/p/FObvS3W4UD

Here's the code from the playground (for convenience):

/*

Output on my machine:

    amd64 darwin go1.3 input: [255 255 255 255]
    -1
    4294967295

Output on the go playground:

    amd64p32 nacl go1.3 input: [255 255 255 255]
    -1
    -1

*/

package main

import (
    "fmt"
    "runtime"
)

func main() {
    input := []byte{255, 255, 255, 255}
    fmt.Println(runtime.GOARCH, runtime.GOOS, runtime.Version(), "input:", input)
    fmt.Println(toInt(input))
    fmt.Println(toInt2(input))
}

func toInt(bytes []byte) int {
    var value int32 = 0 // initialized with int32

    for i, b := range bytes {
        value |= int32(b) << uint(i*8)
    }
    return int(value) // converted to int
}

func toInt2(bytes []byte) int {
    var value int = 0 // initialized with plain old int

    for i, b := range bytes {
        value |= int(b) << uint(i*8)
    }
    return value
}
  • 写回答

2条回答 默认 最新

  • dongxu4580 2014-07-11 22:18
    关注

    This is an educated guess, but int type can be 64bit or 32bit depending on the platform, on my system and yours it's 64bit, since the playground is running on nacl, it's 32bit.

    If you change the 2nd function to use uint all around, it will work fine.

    From the spec:

    uint     either 32 or 64 bits 
    int      same size as uint
    uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题