diff --git a/docs/convertor.md b/docs/convertor.md index 0ab0714..e0bd358 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -34,6 +34,8 @@ import ( - [ToString](#ToString) - [StructToMap](#StructToMap) - [MapToSlice](#MapToSlice) +- [EncodeByte](#EncodeByte) +- [DecodeByte](#DecodeByte)
@@ -457,4 +459,59 @@ func main() { fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} } +``` + + +### EncodeByte + +

Encode data to byte slice.

+ +Signature: + +```go +func EncodeByte(data any) ([]byte, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + byteData, _ := convertor.EncodeByte("abc") + fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} +} +``` + + + +### DecodeByte + +

Decode byte data to target object. target should be a pointer instance.

+ +Signature: + +```go +func DecodeByte(data []byte, target any) error +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + var result string + byteData := []byte{6, 12, 0, 3, 97, 98, 99} + convertor.DecodeByte(byteData, &result) + fmt.Println(result) //"abc" +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index b0e0240..2ec8d7d 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -36,6 +36,8 @@ import ( - [ToString](#ToString) - [StructToMap](#StructToMap) - [MapToSlice](#MapToSlice) +- [EncodeByte](#EncodeByte) +- [DecodeByte](#DecodeByte)
@@ -489,3 +491,59 @@ func main() { fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} } ``` + + + +### EncodeByte + +

将data编码成字节切片

+ +函数签名: + +```go +func EncodeByte(data any) ([]byte, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + byteData, _ := convertor.EncodeByte("abc") + fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} +} +``` + + + +### DecodeByte + +

解码字节切片到目标对象,目标对象需要传入一个指针实例子

+ +函数签名: + +```go +func DecodeByte(data []byte, target any) error +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + var result string + byteData := []byte{6, 12, 0, 3, 97, 98, 99} + convertor.DecodeByte(byteData, &result) + fmt.Println(result) //"abc" +} +``` \ No newline at end of file