var i8 int8 = 113
//转化为16位
var i16 int16 = int16(i8)
var i32 int32 = int32(i16)
var i64 int64 = int64(i32)
var i642 int64 = int64(i16)
var i641 int64 = int64(i8)
var i int = int(i64)
//证书转换: 113 113 113 113 113 113 113
fmt.Println("证书转换:", i8, i16, i32, i64, i642, i641, i)
//浮点之间转换
var f32 float32 = 456.8903
var f64 float64 = float64(f32)
//整数转浮点
var f642 float64 = float64(i32)
var f443 float64 = float64(i16)
var f444 float64 = float64(i8)
var f445 float64 = float64(i)
//浮点数转换: 456.8903 456.8902893066406 113 113 113 113
fmt.Println("浮点数转换:", f32, f64, f642, f443, f444, f445)
//浮点转整数
var i82 int8 = int8(f32)
var i322 int32 = int32(f32)
fmt.Println("浮点转整数:", i82, i322) //浮点转整数: -56 456
//数字转字符
str1 := strconv.FormatInt(int64(i8), 10)
str2 := strconv.FormatInt(int64(i16), 10)
str3 := strconv.FormatInt(int64(i32), 10)
str4 := strconv.FormatInt(i64, 10)
str5 := strconv.FormatInt(int64(i), 10)
// FormatFloat converts the floating-point number f to a string,
// according to the format fmt and precision prec. It rounds the
// result assuming that the original was obtained from a floating-point
// value of bitSize bits (32 for float32, 64 for float64).
//
// The format fmt is one of
// 'b' (-ddddp±ddd, a binary exponent),
// 'e' (-d.dddde±dd, a decimal exponent),
// 'E' (-d.ddddE±dd, a decimal exponent),
// 'f' (-ddd.dddd, no exponent),
// 'g' ('e' for large exponents, 'f' otherwise),
// 'G' ('E' for large exponents, 'f' otherwise),
// 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
// 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
//
// The precision prec controls the number of digits (excluding the exponent)
// printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats.
// For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point.
// For 'g' and 'G' it is the maximum number of significant digits (trailing
// zeros are removed).
// The special precision -1 uses the smallest number of digits
// necessary such that ParseFloat will return f exactly.
str6 := strconv.FormatFloat(float64(f32), 'f', 3, 32)
str7 := strconv.FormatFloat(f64, 'f', 3, 64)
str8 := strconv.Itoa(i)
// 数字转字符串: 113 113 113 113 113 456.890 456.890 113
fmt.Println("数字转字符串:", str1, str2, str3, str4, str5, str6, str7, str8)
str1 = strconv.FormatInt(int64(i8), 16)
str2 = strconv.FormatInt(int64(i16), 16)
str3 = strconv.FormatInt(int64(i32), 16)
str4 = strconv.FormatInt(i64, 16)
str5 = strconv.FormatInt(int64(i), 16)
//转换成16进制 71 71 71 71 71
fmt.Println("转换成16进制", str1, str2, str3, str4, str5)
//字符转数字,注意转换可能会发生error
num1, _ := strconv.ParseInt(str1, 10, 8)
num2, _ := strconv.ParseInt(str2, 10, 16)
num3, _ := strconv.ParseInt(str3, 10, 32)
num4, _ := strconv.ParseInt(str4, 10, 64)
num5, _ := strconv.ParseFloat(str6, 32)
num6, _ := strconv.ParseFloat(str7, 64)
num7, _ := strconv.Atoi(str8)
//字符串转数字: 225 225 225 0 456.8900146484375 456.89 113
fmt.Println("字符串转数字:", num1, num2, num3, num4, num5, num6, num7)
var str = "hello"
var bytes = []byte(str)
var str2 = string(bytes[0:3])
fmt.Println(str, bytes, str2)
float与16进制转换
// StringToBytes 字符串转字节数组
func StringToBytes(s string) []byte {
bytes, _ := hex.DecodeString(s)
return bytes
}
// ByteToFloat32 byte字节数组转float32
func ByteToFloat32(bytes []byte) float32 {
bits := binary.LittleEndian.Uint32(bytes)
return math.Float32frombits(bits)
}
// 小端16进制转float32
func hexToFloat32(hex string) float32 {
bytes := StringToBytes(hex)
return ByteToFloat32(bytes)
}
// 需要传入的hex为大端格式
func hexToFloat(hex string) float32 {
s := hex
n, err := strconv.ParseUint(s, 16, 32)
if err != nil {
fmt.Println(err)
}
n2 := uint32(n)
f := *(*float32)(unsafe.Pointer(&n2))
return f
}
// 10进制转2位字节16进制(15为000f)
func numToHex(num int) string {
hex := fmt.Sprintf("%04x", num)
return hex
}
// 浮点型转16进制字符串(小端模式16进制字符串)
func floatToHex(float32 float32) string {
// 浮点型转字节数组
bits := math.Float32bits(float32)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, bits)
// 字节数组转16进制字符串
hex := hex.EncodeToString(bytes)
return hex
}