初始化项目
This commit is contained in:
58
trunk/goutil/intAndBytesUtil/bytes.go
Normal file
58
trunk/goutil/intAndBytesUtil/bytes.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package intAndBytesUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// 字节数组转换成整形
|
||||
// b:字节数组
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的int值
|
||||
func BytesToInt(b []byte, order binary.ByteOrder) int {
|
||||
bytesBuffer := bytes.NewBuffer(b)
|
||||
|
||||
var result int
|
||||
binary.Read(bytesBuffer, order, &result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 字节数组转换成整形
|
||||
// b:字节数组
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的int16值
|
||||
func BytesToInt16(b []byte, order binary.ByteOrder) int16 {
|
||||
bytesBuffer := bytes.NewBuffer(b)
|
||||
|
||||
var result int16
|
||||
binary.Read(bytesBuffer, order, &result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 字节数组转换成整形
|
||||
// b:字节数组
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的int32值
|
||||
func BytesToInt32(b []byte, order binary.ByteOrder) int32 {
|
||||
bytesBuffer := bytes.NewBuffer(b)
|
||||
|
||||
var result int32
|
||||
binary.Read(bytesBuffer, order, &result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 字节数组转换成整形
|
||||
// b:字节数组
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的int64值
|
||||
func BytesToInt64(b []byte, order binary.ByteOrder) int64 {
|
||||
bytesBuffer := bytes.NewBuffer(b)
|
||||
|
||||
var result int64
|
||||
binary.Read(bytesBuffer, order, &result)
|
||||
|
||||
return result
|
||||
}
|
||||
70
trunk/goutil/intAndBytesUtil/bytes_test.go
Normal file
70
trunk/goutil/intAndBytesUtil/bytes_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package intAndBytesUtil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBytesToInt(t *testing.T) {
|
||||
var givenBigEndian []byte = []byte{0, 0, 1, 0}
|
||||
var givenLittleEndian []byte = []byte{0, 1, 0, 0}
|
||||
var expectedInt int32 = 256
|
||||
|
||||
result := BytesToInt32(givenBigEndian, binary.BigEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenBigEndian, result, expectedInt)
|
||||
}
|
||||
|
||||
result = BytesToInt32(givenLittleEndian, binary.LittleEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenLittleEndian, result, expectedInt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesToInt16(t *testing.T) {
|
||||
var givenBigEndian []byte = []byte{1, 0}
|
||||
var givenLittleEndian []byte = []byte{0, 1}
|
||||
var expectedInt int16 = 256
|
||||
|
||||
result := BytesToInt16(givenBigEndian, binary.BigEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenBigEndian, result, expectedInt)
|
||||
}
|
||||
|
||||
result = BytesToInt16(givenLittleEndian, binary.LittleEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenLittleEndian, result, expectedInt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesToInt32(t *testing.T) {
|
||||
var givenBigEndian []byte = []byte{0, 0, 1, 0}
|
||||
var givenLittleEndian []byte = []byte{0, 1, 0, 0}
|
||||
var expectedInt int32 = 256
|
||||
|
||||
result := BytesToInt32(givenBigEndian, binary.BigEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenBigEndian, result, expectedInt)
|
||||
}
|
||||
|
||||
result = BytesToInt32(givenLittleEndian, binary.LittleEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenLittleEndian, result, expectedInt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesToInt64(t *testing.T) {
|
||||
var givenBigEndian []byte = []byte{0, 0, 0, 0, 0, 0, 1, 0}
|
||||
var givenLittleEndian []byte = []byte{0, 1, 0, 0, 0, 0, 0, 0}
|
||||
var expectedInt int64 = 256
|
||||
|
||||
result := BytesToInt64(givenBigEndian, binary.BigEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenBigEndian, result, expectedInt)
|
||||
}
|
||||
|
||||
result = BytesToInt64(givenLittleEndian, binary.LittleEndian)
|
||||
if result != expectedInt {
|
||||
t.Errorf("BytesToInt(%v) failed.Got %v, expected %v", givenLittleEndian, result, expectedInt)
|
||||
}
|
||||
}
|
||||
4
trunk/goutil/intAndBytesUtil/doc.go
Normal file
4
trunk/goutil/intAndBytesUtil/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
提供int和[]Bytes互相转化的助手方法,其中需要注意的是在不同的平台上大、小端是不同的
|
||||
*/
|
||||
package intAndBytesUtil
|
||||
50
trunk/goutil/intAndBytesUtil/int.go
Normal file
50
trunk/goutil/intAndBytesUtil/int.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package intAndBytesUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// 整形转换成字节(无效,因为系统无法判断读取的字节数)
|
||||
// n:int型数字
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的字节数组
|
||||
// func IntToBytes(n int, order binary.ByteOrder) []byte {
|
||||
// bytesBuffer := bytes.NewBuffer([]byte{})
|
||||
// binary.Write(bytesBuffer, order, n)
|
||||
|
||||
// return bytesBuffer.Bytes()
|
||||
// }
|
||||
|
||||
// 整形转换成字节
|
||||
// n:int16型数字
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的字节数组
|
||||
func Int16ToBytes(n int16, order binary.ByteOrder) []byte {
|
||||
bytesBuffer := bytes.NewBuffer([]byte{})
|
||||
binary.Write(bytesBuffer, order, n)
|
||||
|
||||
return bytesBuffer.Bytes()
|
||||
}
|
||||
|
||||
// 整形转换成字节
|
||||
// n:int32型数字
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的字节数组
|
||||
func Int32ToBytes(n int32, order binary.ByteOrder) []byte {
|
||||
bytesBuffer := bytes.NewBuffer([]byte{})
|
||||
binary.Write(bytesBuffer, order, n)
|
||||
|
||||
return bytesBuffer.Bytes()
|
||||
}
|
||||
|
||||
// 整形转换成字节
|
||||
// n:int64型数字
|
||||
// order:大、小端的枚举
|
||||
// 返回值:对应的字节数组
|
||||
func Int64ToBytes(n int64, order binary.ByteOrder) []byte {
|
||||
bytesBuffer := bytes.NewBuffer([]byte{})
|
||||
binary.Write(bytesBuffer, order, n)
|
||||
|
||||
return bytesBuffer.Bytes()
|
||||
}
|
||||
68
trunk/goutil/intAndBytesUtil/int_test.go
Normal file
68
trunk/goutil/intAndBytesUtil/int_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package intAndBytesUtil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInt16ToBytes(t *testing.T) {
|
||||
var expectedBigEndian []byte = []byte{1, 0}
|
||||
var expectedLittleEndian []byte = []byte{0, 1}
|
||||
var givenInt int16 = 256
|
||||
|
||||
result := Int16ToBytes(givenInt, binary.BigEndian)
|
||||
if equal(result, expectedBigEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedBigEndian)
|
||||
}
|
||||
|
||||
result = Int16ToBytes(givenInt, binary.LittleEndian)
|
||||
if equal(result, expectedLittleEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedLittleEndian)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32ToBytes(t *testing.T) {
|
||||
var expectedBigEndian []byte = []byte{0, 0, 1, 0}
|
||||
var expectedLittleEndian []byte = []byte{0, 1, 0, 0}
|
||||
var givenInt int32 = 256
|
||||
|
||||
result := Int32ToBytes(givenInt, binary.BigEndian)
|
||||
if equal(result, expectedBigEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedBigEndian)
|
||||
}
|
||||
|
||||
result = Int32ToBytes(givenInt, binary.LittleEndian)
|
||||
if equal(result, expectedLittleEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedLittleEndian)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64ToBytes(t *testing.T) {
|
||||
var expectedBigEndian []byte = []byte{0, 0, 0, 0, 0, 0, 1, 0}
|
||||
var expectedLittleEndian []byte = []byte{0, 1, 0, 0, 0, 0, 0, 0}
|
||||
var givenInt int64 = 256
|
||||
|
||||
result := Int64ToBytes(givenInt, binary.BigEndian)
|
||||
if equal(result, expectedBigEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedBigEndian)
|
||||
}
|
||||
|
||||
result = Int64ToBytes(givenInt, binary.LittleEndian)
|
||||
if equal(result, expectedLittleEndian) == false {
|
||||
t.Errorf("IntToBytes(%v) failed.Got %v, expected %v", givenInt, result, expectedLittleEndian)
|
||||
}
|
||||
}
|
||||
|
||||
func equal(b1, b2 []byte) bool {
|
||||
if len(b1) != len(b2) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < len(b1); i++ {
|
||||
if b1[i] != b2[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user