From 04ab5cfb262c8dbdadf2090bde99506ff2eac5a9 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 25 Sep 2024 23:57:01 +0700 Subject: lib/bytes: add function AppendInt64 and AppendUint64 The AppendInt64 append an int64 value into slice of byte. The AppendUint64 append an uint64 value into slice of byte. --- lib/bytes/bytes.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'lib/bytes/bytes.go') diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 6e44f0a6..b47b0a85 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -1,6 +1,6 @@ -// Copyright 2018, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan +// +// SPDX-License-Identifier: BSD-3-Clause // Package bytes provide a library for working with byte or slice of bytes. package bytes @@ -32,6 +32,19 @@ func AppendInt32(data []byte, v int32) []byte { return data } +// AppendInt64 append an int64 value into slice of byte. +func AppendInt64(data []byte, v int64) []byte { + data = append(data, byte(v>>56)) + data = append(data, byte(v>>48)) + data = append(data, byte(v>>40)) + data = append(data, byte(v>>32)) + data = append(data, byte(v>>24)) + data = append(data, byte(v>>16)) + data = append(data, byte(v>>8)) + data = append(data, byte(v)) + return data +} + // AppendUint16 append an uint16 value into slice of byte. func AppendUint16(data []byte, v uint16) []byte { data = append(data, byte(v>>8)) @@ -48,6 +61,19 @@ func AppendUint32(data []byte, v uint32) []byte { return data } +// AppendUint64 append an uint64 value into slice of byte. +func AppendUint64(data []byte, v uint64) []byte { + data = append(data, byte(v>>56)) + data = append(data, byte(v>>48)) + data = append(data, byte(v>>40)) + data = append(data, byte(v>>32)) + data = append(data, byte(v>>24)) + data = append(data, byte(v>>16)) + data = append(data, byte(v>>8)) + data = append(data, byte(v)) + return data +} + // Concat merge one or more []byte or string in args into slice of // byte. // Any type that is not []byte or string in args will be ignored. -- cgit v1.3-5-g45d5