diff --git a/buffer.go b/buffer.go new file mode 100644 index 0000000..b1e5d57 --- /dev/null +++ b/buffer.go @@ -0,0 +1,39 @@ +package utils + +import "strings" + + +const ( + hextable = "0123456789abcdef" +) + +type Buffer []byte + +func NewBuffer() Buffer { + return make([]byte,0) +} + +func (b Buffer) Length() int{ + return len(b) // the unint is byte +} + +func (b Buffer) String() string { + str := make([]byte,b.Length() << 1) + j := 0 + for _,item := range b { + str[j] = hextable[item>>4] + str[j+1] = hextable[item&0x0f] + j += 2 + } + return string(str) +} + +func (b Buffer) Load(data string) Buffer{ + for i:= 0;i < len(data);i+=2 { + cache := 0x00 + cache = strings.Index(hextable,string(data[i])) << 4 + cache = cache | strings.Index(hextable,string(data[i+1])) + b = append(b, byte(cache)) + } + return b +} diff --git a/utils_test.go b/utils_test.go index d9fa886..93738ee 100644 --- a/utils_test.go +++ b/utils_test.go @@ -326,6 +326,14 @@ func TestDateTime(t *testing.T) { t.Error("caculate has wrong") } + datetime2 := utils.NewDateTime() + + datetime2 = *datetime2.Parse("2024/06/06/23:10:40","YYYY/MM/DD/HH:mm:ss") + + if datetime2.Year != 2024 { + t.Error("paser has wrong") + } + fmt.Printf("解析后时间 %s",datetime2.String()) } func TestNumberConver(t *testing.T) { @@ -441,8 +449,6 @@ func TestReferenceString(t *testing.T){ func TestBuffer(t *testing.T){ bf := utils.NewBuffer() bf = bf.Load("320c3a83c202880e83fa0814320c3a83c202880e83fa10") - fmt.Printf("%#v\n",bf) - if bf[0] != 0x32 { t.Error(fmt.Sprintf("TestBuffer expect: %d,but got %d",0x32,bf[0])) }