在Golang中将字节流转换为字节流片段的最佳方式是什么?目前我正在尝试在golang中复制一个java程序,我相信我有一些事实,即java将字节流作为有符号值读取,而golang将其视为无符号值。将无符号字节流转换为有符号字节流Golang

当我用Java打印时,负值是不同的。正面都是一样的:

的Java:

8 | -8 | -58 | -61 | -56 | -113 | 42 | 16 | -64 | 2 | 24 | -16 | 1

Golang:

8 | | | 195 | 200 | 143 | 42 | 16 | 192 | 2 | 2 | 240 | 1

目前,GO我执行权的样子:

//open the file with os.open 
//create new reader and unzip it 

//reads from zip file and returns an array of bytes associated with it. 
data, err := ioutil.ReadAll(fz) 
if err != nil { 
    return nil, err 
} 
//reflect.TypeOf shows this is a []uint8 
//I want this to be a []int8 (so signed). 

在Java中实现是非常多的:

//create buffer reader 
    //create input stream 
    //create datainput stream 
    //use the .Read function to get the values required. 

我没有看到任何简单的方法来快速将它转换为signed int(也许我错了)。我可以尝试迭代整个切片,将每个值转换为带符号的int,但是这种方法似乎相当混乱。这也需要我对每一个进行操作。有没有一种更清晰的方式来转换切片?

2016-05-22 and0rsk

+0

http://stackoverflow.com/q/12357865/4740606和http://stackoverflow.com/q/28949063/4740606的最佳答案可能对您有所帮助 –