/* * Hexacode Types * Package: gitlab.hexacode.org/go-libs/hctypes * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package hctypes import ( "bytes" "encoding/binary" "encoding/hex" "math/big" ) type ( Buffer []byte Float32 float32 Float64 float64 Bool bool Int8 int8 Int16 int16 Int32 int32 Int64 int64 UInt8 uint8 UInt16 uint16 UInt32 uint32 UInt64 uint64 BigInt big.Int String string ) func (this Buffer) Equal(buf Buffer) Bool { return Bool(bytes.Equal(this, buf)) } func (this Buffer) ToString() String { return String(this) } func (this Buffer) Encode() String { return String(hex.EncodeToString(this)) } func (this Buffer) ToFloat32() Float32 { var ret Float32 binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret) return ret } func (this Buffer) ToFloat64() Float64 { var ret Float64 binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret) return ret } func (this Buffer) ToBool() Bool { i := UInt8([]byte(this)[0]) if i == 1 { return true } else { return false } } func (this Buffer) ToInt8() Int8 { return Int8([]byte(this)[0]) } func (this Buffer) ToInt16() Int16 { var ret Int16 binary.Read(bytes.NewReader([]byte(this)[:2]), binary.BigEndian, &ret) return ret } func (this Buffer) ToInt32() Int32 { var ret Int32 binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret) return ret } func (this Buffer) ToInt64() Int64 { var ret Int64 binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret) return ret } // ------- func (this Buffer) ToUInt8() UInt8 { return UInt8([]byte(this)[0]) } func (this Buffer) ToUInt16() UInt16 { var ret UInt16 binary.Read(bytes.NewReader([]byte(this)[:2]), binary.BigEndian, &ret) return ret } func (this Buffer) ToUInt32() UInt32 { var ret UInt32 binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret) return ret } func (this Buffer) ToUInt64() UInt64 { var ret UInt64 binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret) return ret } func (this Buffer) ToBigInt() BigInt { var ret = big.Int{} ret.SetBytes(this) return BigInt(ret) } // ------- func (this String) ToBuffer() Buffer { return Buffer(this) } func (this String) Decode() Buffer { data, err := hex.DecodeString(string(this)) if err != nil { return Buffer([]byte{}) } return Buffer(data) } func (this Float32) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this Float64) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this Bool) ToBuffer() Buffer { b := UInt8(0) if this { b = UInt8(1) } return b.ToBuffer() } func (this Int8) ToBuffer() Buffer { return Buffer{byte(this)} } func (this Int16) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this Int32) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this Int64) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } // ------------ func (this UInt8) ToBuffer() Buffer { return Buffer{byte(this)} } func (this UInt16) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this UInt32) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this UInt64) ToBuffer() Buffer { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, this) return buf.Bytes() } func (this BigInt) ToBuffer() Buffer { bi := big.Int(this) return bi.Bytes() }