mirror of https://github.com/siddontang/go.git
update num
This commit is contained in:
parent
032f1ed536
commit
466d5bc779
30
num/bytes.go
30
num/bytes.go
|
@ -35,3 +35,33 @@ func Uint64ToBytes(u uint64) []byte {
|
|||
binary.BigEndian.PutUint64(buf, u)
|
||||
return buf
|
||||
}
|
||||
|
||||
func BytesToInt16(b []byte) int16 {
|
||||
return int16(binary.BigEndian.Uint16(b))
|
||||
}
|
||||
|
||||
func Int16ToBytes(u int16) []byte {
|
||||
buf := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(buf, uint16(u))
|
||||
return buf
|
||||
}
|
||||
|
||||
func BytesToInt32(b []byte) int32 {
|
||||
return int32(binary.BigEndian.Uint32(b))
|
||||
}
|
||||
|
||||
func Int32ToBytes(u int32) []byte {
|
||||
buf := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(buf, uint32(u))
|
||||
return buf
|
||||
}
|
||||
|
||||
func BytesToInt64(b []byte) int64 {
|
||||
return int64(binary.BigEndian.Uint64(b))
|
||||
}
|
||||
|
||||
func Int64ToBytes(u int64) []byte {
|
||||
buf := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(buf, uint64(u))
|
||||
return buf
|
||||
}
|
||||
|
|
|
@ -144,6 +144,18 @@ func TestBytes(t *testing.T) {
|
|||
if BytesToUint16(Uint16ToBytes(1)) != 1 {
|
||||
t.Fatal("convert fail")
|
||||
}
|
||||
|
||||
if BytesToInt64(Int64ToBytes(-1)) != -1 {
|
||||
t.Fatal("convert fail")
|
||||
}
|
||||
|
||||
if BytesToInt32(Int32ToBytes(-1)) != -1 {
|
||||
t.Fatal("convert fail")
|
||||
}
|
||||
|
||||
if BytesToInt16(Int16ToBytes(-1)) != -1 {
|
||||
t.Fatal("convert fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStr(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue