convertoper
package convertoper
import (
"encoding/binary"
"reflect"
"unsafe"
)
//byte数组转 int32
func BytesToInt32(buf []byte) int32 {
return int32(binary.BigEndian.Uint32(buf))
}
//byte数组转 int64
func BytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}
//https://www.cnblogs.com/ghost240/p/3739785.html
//string 转换成byte[]
func StringTobytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{
x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func SliceToString(b []byte) (s string) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
}
//利用反射来转切片为string
func SliceToStringByintefce(v interface{
}) (s string) {
/*
if reflect.TypeOf(v).Kind()!=reflect.Slice {
otheroper.ErrorMsg(errors.New("当前类型不是Slice"),2,"")
return
}
*/
return string(reflect.ValueOf(v).Bytes())
}
//https://www.cnblogs.com/Detector/p/9686443.html
// 字符首字母大写
func FirstUpper(str string) string {
var upperStr string
vv := []rune(str) // 后文有介绍
for i := 0; i < len(vv); i++ {
if i == 0 {
if vv[i] >= 97 && vv[i] <= 122 {
// 后文有介绍
vv[i] -= 32 // string的码表相差32位
upperStr += string(vv[i])
} else {
//fmt.Println("Not begins with lowercase letter,")
return str
}
} else {
upperStr += string(vv[i])
}
}
return upperStr
}
fileoper
package fileoper
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"tools/otheroper"
)
//复制文件