-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
49 lines (44 loc) · 907 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
)
func main() {
addr := "255.255.255.255:65535"
b := bytes.Buffer{}
b.WriteString(addr)
b.WriteString("|")
b.WriteString("asdf")
// string()
c := b.Bytes()
s := string(c)
fmt.Println(s)
// fmt.Println([]byte())
// fmt.Println([]byte("255.255.255.255:1"))
}
func AddrToString(addr []byte) {
s0 := string(int(addr[0]))
s1 := string(int(addr[1]))
s := []string{s0, s1}
fmt.Println(strings.Join(s, "."))
}
//AddrToBytes 字符串地址转换为字节
func AddrToBytes(addr string) []byte {
temp := strings.Split(addr, ":")
parts := strings.Split(temp[0], ".")
b0, err := strconv.Atoi(parts[0])
if err != nil {
return nil
}
b1, _ := strconv.Atoi(parts[1])
b2, _ := strconv.Atoi(parts[2])
b3, _ := strconv.Atoi(parts[3])
b := make([]byte, 4)
b[0] = byte(b0)
b[1] = byte(b1)
b[2] = byte(b2)
b[3] = byte(b3)
return b
}