From 52462da6b4b356e3e88c16d614d596461674b01b Mon Sep 17 00:00:00 2001 From: guzeng Date: Mon, 8 Aug 2022 11:17:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=81=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=E8=BD=AC=E5=8D=81=E5=85=AD=E8=BF=9B=E5=88=B6=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- math.go | 27 +++++++++++++++++++++++++++ math_test.go | 6 ++++++ string_test.go | 6 +++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/math.go b/math.go index 75d903e..341ab3e 100755 --- a/math.go +++ b/math.go @@ -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, "") +} diff --git a/math_test.go b/math_test.go index 3c43f03..a550fd5 100644 --- a/math_test.go +++ b/math_test.go @@ -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) +} diff --git a/string_test.go b/string_test.go index 0395baf..6f4dddd 100755 --- a/string_test.go +++ b/string_test.go @@ -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) }