123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package gopay
- import (
- "math/rand"
- "strconv"
- "time"
- )
- // 获取随机字符串
- // length:字符串长度
- func GetRandomString(length int) string {
- str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
- var (
- result []byte
- b []byte
- r *rand.Rand
- )
- b = []byte(str)
- r = rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < length; i++ {
- result = append(result, b[r.Intn(len(b))])
- }
- return string(result)
- }
- // 解析时间
- func ParseDateTime(timeStr string) (datetime time.Time) {
- datetime, _ = time.ParseInLocation(TimeLayout, timeStr, time.Local)
- return
- }
- // 字符串转Float64
- func String2Float64(floatStr string) (floatNum float64) {
- floatNum, _ = strconv.ParseFloat(floatStr, 64)
- return
- }
- // 字符串转Float32
- func String2Float32(floatStr string) (floatNum float32) {
- float, _ := strconv.ParseFloat(floatStr, 32)
- floatNum = float32(float)
- return
- }
- // Float64转字符串
- // floatNum:float64数字
- // prec:精度位数(不传则默认float数字精度)
- func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
- if len(prec) > 0 {
- floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
- return
- }
- floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
- return
- }
- // Float32转字符串
- // floatNum:float32数字
- // prec:精度位数(不传则默认float数字精度)
- func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
- if len(prec) > 0 {
- floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
- return
- }
- floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
- return
- }
- // 字符串转Int
- func String2Int(intStr string) (intNum int) {
- intNum, _ = strconv.Atoi(intStr)
- return
- }
- // 字符串转Int32
- func String2Int32(intStr string) (int32Num int32) {
- intNum, _ := strconv.Atoi(intStr)
- int32Num = int32(intNum)
- return
- }
- // 字符串转Int64
- func String2Int64(intStr string) (int64Num int64) {
- intNum, _ := strconv.Atoi(intStr)
- int64Num = int64(intNum)
- return
- }
- // Int转字符串
- func Int2String(intNum int) (intStr string) {
- intStr = strconv.Itoa(intNum)
- return
- }
- // Int32转字符串
- func Int322String(intNum int32) (int32Str string) {
- // 10, 代表10进制
- int32Str = strconv.FormatInt(int64(intNum), 10)
- return
- }
- // Int64转字符串
- func Int642String(intNum int64) (int64Str string) {
- // 10, 代表10进制
- int64Str = strconv.FormatInt(intNum, 10)
- return
- }
- // 解密填充模式(去除补全码) PKCS7UnPadding
- // 解密时,需要在最后面去掉加密时添加的填充byte
- func PKCS7UnPadding(origData []byte) (bs []byte) {
- length := len(origData)
- unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte 数字
- if unPaddingNumber <= 16 {
- bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
- } else {
- bs = origData
- }
- return
- }
- // 解密填充模式(去除补全码) PKCS5UnPadding
- // 解密时,需要在最后面去掉加密时添加的填充byte
- func PKCS5UnPadding(origData []byte) (bs []byte) {
- length := len(origData)
- unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte
- if unPaddingNumber <= 16 {
- bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
- } else {
- bs = origData
- }
- return
- }
|