40 lines
746 B
Go
40 lines
746 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"encoding/binary"
|
|
"strconv"
|
|
)
|
|
|
|
// Datatype_Int manages the int data type
|
|
type Datatype_Int64 struct{}
|
|
|
|
func (t *Datatype_Int64) Name() string {
|
|
return "int"
|
|
}
|
|
|
|
func (t *Datatype_Int64) ToString(data []byte) (string, error) {
|
|
return fmt.Sprintf("%d", t.btoi(data)), nil
|
|
}
|
|
|
|
func (t *Datatype_Int64) FromString(val string) ([]byte, error) {
|
|
v, err := strconv.ParseInt(val, 10, 64)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
return t.itob(v), nil
|
|
}
|
|
|
|
func (t *Datatype_Int64) itob(v int64) []byte {
|
|
b := make([]byte, 8)
|
|
binary.BigEndian.PutUint64(b, uint64(v))
|
|
return b
|
|
}
|
|
|
|
func (t *Datatype_Int64) btoi(b []byte) int64 {
|
|
if len(b) < 8 {
|
|
return 0
|
|
}
|
|
return int64(binary.BigEndian.Uint64(b))
|
|
}
|