-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
44 lines (40 loc) · 877 Bytes
/
strings.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
// Byteman
// For the full copyright and license information, please view the LICENSE.txt file.
package byteman
import (
"encoding/hex"
)
// FromString returns a byte slice by the given string and desired byte size.
func FromString(str string, size int) []byte {
if size == 0 {
size = len(str)
} else if size < 0 {
if ns := len(str) + size; ns > 0 {
size = ns
} else {
size = 0
}
}
b := make([]byte, size)
copy(b, []byte(str))
return b
}
// FromHex returns a byte slice by the given ASCII Hex code and desired byte size.
func FromHex(hexcode string, size int) []byte {
decoded, err := hex.DecodeString(hexcode)
if err != nil {
return nil
}
if size == 0 {
size = len(decoded)
} else if size < 0 {
if ns := len(decoded) + size; ns > 0 {
size = ns
} else {
size = 0
}
}
b := make([]byte, size)
copy(b, []byte(decoded))
return b
}