go 进制互转
十进制 <------> 十六进制
//10---->16
func Base10ToBase16(base10 []byte)(string,error){
//base10 := 12
//不自动补0,如12----> C
//base16 := fmt.Sprintf("%X", string(base10))
//自动补0,如12----> 0c
base16 := hex.EncodeToString([]byte(base10))
//fmt.Printf("十进制(%d)转十六进制:%v\n", base10, base16)
return base16,nil
}
//16---->10
func Base16ToBase10(base16 []byte)(string,error){
//base16 = "3e8"
base16Str := fmt.Sprintf("%s", base16)
base10, err:= strconv.ParseUint(base16Str, 16, 32) //十六进制转10进制
if err!=nil{
return "",err
}
//fmt.Printf("十六进制(%v)转十进制:%v\n", base16, base10)
return base10,nil
}