8
go int 和 []byte互相转化
source link: https://studygolang.com/articles/10570?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
go int 和 []byte互相转化
jinjiashan · 2017-08-06 03:59:04 · 11461 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览这是一个创建于 2017-08-06 03:59:04 的文章,其中的信息可能已经有所发展或是发生改变。
1,Int2Byte
把int的每个字节取出来放入byte数组中,存储采用Littledian
2,Byte2Int
把byte Slice 中的每个字节取出来, 按Littledian端拼成一个int
func Int2Byte(data int)(ret []byte){ var len uintptr = unsafe.Sizeof(data) ret = make([]byte, len) var tmp int = 0xff var index uint = 0 for index=0; index<uint(len); index++{ ret[index] = byte((tmp<<(index*8) & data)>>(index*8)) } return ret } func Byte2Int(data []byte)int{ var ret int = 0 var len int = len(data) var i uint = 0 for i=0; i<uint(len); i++{ ret = ret | (int(data[i]) << (i*8)) } return ret }
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK