diff --git a/pkg/structure/value.go b/pkg/structure/value.go index 3ab4fd4..3dfebb0 100644 --- a/pkg/structure/value.go +++ b/pkg/structure/value.go @@ -3,6 +3,7 @@ package structure import ( "bytes" "encoding/binary" + "fmt" "math" "strconv" @@ -175,12 +176,34 @@ func (v *Value) GetByte(offset int) (bool, error) { } func (v *Value) SliceByString(start, end int) ([]byte, error) { - if v.onType != DynamicString { - return nil, errorx.New("not is string") - } if start > end { return nil, errorx.New("the end cannot be greater than the beginning") } + + if v.onType == DynamicInt { + ret, err := v.ToInt() + if err != nil { + return nil, err + } + value := strconv.Itoa(int(ret)) + if end > len(value) { + return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value)) + } + return []byte(value[start:end]), nil + } + + if v.onType == DynamicFloat { + ret, err := v.ToFloat64() + if err != nil { + return nil, err + } + value := fmt.Sprintf("%.2f", ret) + if end > len(value) { + return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value)) + } + return []byte(value[start:end]), nil + } + if end > v.length { return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length) }