boltbrowser/datatype_uint.go

38 lines
801 B
Go

package main
import (
"encoding/binary"
"errors"
"strconv"
)
// Datatype_Uint64 manages the uint64 data type
// All conversions just use the binary package
type Datatype_Uint64 struct{}
func (t *Datatype_Uint64) Name() string {
return "uint64"
}
func (t *Datatype_Uint64) ToString(data []byte) (string, error) {
ret, bt := binary.Uvarint(data)
if bt == 0 {
return "", errors.New("Byte Buffer too small")
} else if bt < 0 {
return "", errors.New("Value larger than 64 bits")
}
return strconv.FormatUint(ret, 10), nil
}
func (t *Datatype_Uint64) FromString(val string) ([]byte, error) {
var err error
var u uint64
buf := make([]byte, binary.MaxVarintLen64)
u, err = strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, err
}
binary.PutUvarint(buf, u)
return buf, nil
}