array > slice

arr := [3]int{1,2,3}
sli := arr[:]

hex > int, big.Int

1
2
3
4
5
6
// int
n, err := strconv.ParseUint(val, 16, 32)

// big.Int
n := new(big.Int)
n, _ = n.SetString(hex[2:], 16)

float > int

1
2
3
4
5
6
//float64 转成转成int64
var x float64 = 5.7
var y int = int64(x)

//
int(math.Floor(f + 0.5))

base64 > hex

1
2
3
4
5
6
p, err := base64.StdEncoding.DecodeString("QVJWSU4=")
if err != nil {
    // handle error
}
h := hex.EncodeToString(p)
fmt.Println(h) // prints 415256494e

bytes > hex

1
hex.EncodeToString(foo)

string, float

1
2
3
4
5
6
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

// float64 > string
valueStr = strconv.FormatFloat(v, 'f', 3, 64)

int > float

1
2
3
i:=5
f:=float32(i)

bytes > int

1
2
3
4
5
6
7
var ba = []byte{ 56, 66, 73, 77 }
var value int32
value |= int32(ba[0])
value |= int32(ba[1]) << 8
value |= int32(ba[2]) << 16
value |= int32(ba[3]) << 24
reverse the indexing order to switch between big and little endian.

int8 > byte

因为两者间的类型及取值范围这些都不相同,不能直接进行转换。int8取值范围为:-128~127,如果要转化的话需要使用bytevalue=256+int8value

1
2
3
4
5
6
7
8
var r byte
var v int8
v = -70
if v < 0 {
    r = byte(256 + int(v))
} else {
    r = byte(v)
}

但是,实际上我们可以直接使用byte进行强制转换,因为byte会自动检测v原有类型,然后进行转换的。

1
2
3
4
var r byte
var v int8
v = -70
r = byte(v)

string, bool

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// string > bool
// 将字符串转换为布尔值
// 它接受真值:1, t, T, TRUE, true, True
// 它接受假值:0, f, F, FALSE, false, False
// 其它任何值都返回一个错误。
func ParseBool(str string) (bool, error)
strconv.ParseBool("false")

// bool > string
// 将布尔值转换为字符串 true 或 false
func FormatBool(b bool) string


int, string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// string到int
int,err:=strconv.Atoi(string)
// string到int64
int64, err := strconv.ParseInt(string, 10, 64)
// int到string
string:=strconv.Itoa(int)
// int64到string
string:=strconv.FormatInt(int64,10)

// int32 to string
 strconv.FormatInt(int64(i), 10)

// int to int32
int32(i)

// uint8 > string
	var s uint8 = 10
	fmt.Print("out: "+strconv.Itoa(int(s)))

// uint64 > string
str := strconv.FormatUint(myNumber, 10)

https://stackoverflow.com/questions/39442167/convert-int32-to-string-in-golang

int32 > uint16

1
2
var i int
u := uint16(i)

string > []byte

1
dataByte:=[]byte(dataStr)

Golang Time互转秒、毫秒

1
2
3
4
5
6
    fmt.Println(time.Now().Unix()) //获取当前秒
    fmt.Println(time.Now().UnixNano())//获取当前纳秒
    fmt.Println(time.Now().UnixNano()/1e6)//将纳秒转换为毫秒
    fmt.Println(time.Now().UnixNano()/1e9)//将纳秒转换为秒
    c := time.Unix(time.Now().UnixNano()/1e9,0) //将秒转换为 time 类型
    fmt.Println(c.String()) //输出当前英文时间戳格式

byte > binary string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "fmt"

func main() {
    bs := []byte{0x00, 0xfd}
    for _, n := range(bs) {
        fmt.Printf("% 08b", n) // prints 00000000 11111101
    }
}

hex string > []byte

1
2
3
4
5
import "hex"

hexStr := "fee9ecaadafeee72d2eb66a0bd344cdd"
data, err := hex.DecodeString(hexStr)

[]byte > hex string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import (
"fmt"
"crypto/md5"
)
// 省略部分代码
data := "test string"
// md5.Sum() return a byte array
h := md5.Sum([]byte(data))

// with "%x" format byte array into hex string
hexStr := fmt.Sprintf("%x", h)

int, byte

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// int64 > bytes
func Int64ToBytes(i int64) []byte {
    var buf = make([]byte, 8)
    binary.BigEndian.PutUint64(buf, uint64(i))
    return buf
}
// bytes to int64
func BytesToInt64(buf []byte) int64 {
    return int64(binary.BigEndian.Uint64(buf))
}
// int32 > bytes
func Int32ToBytes(i int32) []byte {
    fmt.Printf("int32: %v\n", i)
    return Uint32ToBytes(uint32(i))
}
// uint32 > bytes
func Uint32ToBytes(i uint32) []byte {
    fmt.Printf("uint32: %v\n", i)
    var buf = make([]byte, 8)
    binary.BigEndian.PutUint32(buf, i)
    return buf
}

hex > signed int

i, _ := strconv.ParseUint(hexValue, 16, 64)
result = int32(i) / int32(rule.Rate)

a := binary.LittleEndian.Uint64(sampleA)
// If you need int64:
a2 := int64(a)
// If you need int32:
a2 := int32(a)

Converting numeric types into a series of bytes ([]byte) and vice versa is about the endianness. How you interpret the result is entirely up to you. All you need is to assemble a 16-bit, 32-bit or 64-bit value, once it’s done, you can interpret the result as you want. For example if you already have a uint16 value, to use it as a signed value, all you need is a type conversion because the memory layout of an uint16 and int16 is the same (converting from uint16 to int16 doesn’t change the memory representation just the type): https://stackoverflow.com/questions/34701187/go-byte-to-little-big-endian-signed-integer-or-float

http://blog.csdn.net/pkueecser/article/details/50433460 http://www.cnblogs.com/golove/p/3262925.html https://blog.csdn.net/lwldcr/article/details/78722330 https://blog.csdn.net/pingD/article/details/76588648 https://www.reddit.com/r/golang/comments/4xn341/converting_byte_to_int32/