Browse Source

增加十进制转十六进制方法

master v0.4.0
guzeng 2 years ago
parent
commit
52462da6b4
3 changed files with 36 additions and 3 deletions
  1. +27
    -0
      math.go
  2. +6
    -0
      math_test.go
  3. +3
    -3
      string_test.go

+ 27
- 0
math.go View File

@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"strconv"
"strings"
)
//字节自动转换为B/KB/MB/GB
@ -129,3 +130,29 @@ func FloatQuo(str ...interface{}) float64 {
ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
return ret
}
/**
* 十进制转十六进制
*/
func TenToHex(ten int) string {
m := 0
hex := make([]int, 0)
for {
m = ten % 16
ten = ten / 16
if ten == 0 {
hex = append(hex, m)
break
}
hex = append(hex, m)
}
hexStr := []string{}
for i := len(hex) - 1; i >= 0; i-- {
if hex[i] >= 10 {
hexStr = append(hexStr, fmt.Sprintf("%c", 'A'+hex[i]-10))
} else {
hexStr = append(hexStr, fmt.Sprintf("%d", hex[i]))
}
}
return strings.Join(hexStr, "")
}

+ 6
- 0
math_test.go View File

@ -42,3 +42,9 @@ func Test_FloatQuo(t *testing.T) {
ret := FloatQuo(str1, str2, 3)
t.Log(ret)
}
func Test_TenToHex(t *testing.T) {
str1 := 10664
ret := TenToHex(str1)
t.Log(ret)
}

+ 3
- 3
string_test.go View File

@ -42,8 +42,8 @@ func Test_ToString(t *testing.T) {
t.Log(ret)
}
func Test_ToInt(t *testing.T) {
var str interface{} = 45
ret := ToInt(str)
func Test_StrFirstToUpper(t *testing.T) {
var str string = "departmentID"
ret := StrFirstToUpper(str)
t.Log(ret)
}

Loading…
Cancel
Save