diff --git a/sckutils/sckutils.go b/sckutils/sckutils.go index 3d238d7..7c4ef08 100644 --- a/sckutils/sckutils.go +++ b/sckutils/sckutils.go @@ -9,13 +9,26 @@ // and operations. package sckutils -// DataBuffer represents a generic data buffer for smart card operations. -type DataBuffer struct { - Data []byte -} +import ( + "fmt" + "strings" +) // ConvertToHexString converts a byte slice to a hex string. -func ConvertToHexString(data []byte) string { return "" } +func ConvertToHexString(data []byte) string { + const f = "%02X" // Define the format specifier + var b strings.Builder + + // Write each byte in hex format to the builder + for i, v := range data { + if i > 0 { + b.WriteString(":") // Add a colon before each byte except the first one + } + b.WriteString(fmt.Sprintf(f, v)) + } + + return b.String() +} // ParseHexString parses a hex string into a byte slice. func ParseHexString(hexStr string) ([]byte, error) { return nil, nil } @@ -26,8 +39,35 @@ func CheckStatusCode(code byte) bool { return false } // NewDataBuffer creates a new DataBuffer with the provided data. func NewDataBuffer(data []byte) *DataBuffer { return nil } -// Marshal serializes a DataBuffer into a byte slice. -func (db *DataBuffer) Marshal() ([]byte, error) { return nil, nil } - // UnmarshalDataBuffer sets the DataBuffer fields from a byte slice. func UnmarshalDataBuffer(data []byte) (*DataBuffer, error) { return nil, nil } + +// HumanizeBytes converts a size in bytes to a human-readable string in KB, MB, GB, etc. +func HumanizeBytes(bytes int64) string { + const ( + kB int64 = 1 << 10 // 1024 + mB int64 = 1 << 20 // 1048576 + gB int64 = 1 << 30 // 1073741824 + ) + + format := "%.2f %s" + switch { + case bytes < kB: + return fmt.Sprintf("%d B", bytes) + case bytes < mB: + return fmt.Sprintf(format, float64(bytes)/float64(kB), "KB") + case bytes < gB: + return fmt.Sprintf(format, float64(bytes)/float64(mB), "MB") + default: + // When file size is larger than 1 MB + return fmt.Sprintf(format, float64(bytes)/float64(gB), "GB") + } +} + +// DataBuffer represents a generic data buffer for smart card operations. +type DataBuffer struct { + Data []byte +} + +// Marshal serializes a DataBuffer into a byte slice. +func (db *DataBuffer) Marshal() ([]byte, error) { return nil, nil } diff --git a/sckutils/sckutils_test.go b/sckutils/sckutils_test.go new file mode 100644 index 0000000..3505389 --- /dev/null +++ b/sckutils/sckutils_test.go @@ -0,0 +1,52 @@ +// Copyright 2023 The Happy Authors +// Licensed under the Apache License, Version 2.0. +// See the LICENSE file. + +package sckutils + +import ( + "fmt" + "testing" +) + +func TestFormatByteSlice(t *testing.T) { + // Test case 1: An empty byte slice should return an empty string. + emptySlice := []byte{} + result := ConvertToHexString(emptySlice) + expected := "" + if result != expected { + t.Errorf("Expected '%s', but got '%s'", expected, result) + } + + // Test case 2: A byte slice with some data should be formatted as expected. + dataSlice := []byte{72, 101, 108, 108, 111} // ASCII values for "Hello" + result = ConvertToHexString(dataSlice) + expected = "48:65:6C:6C:6F" // Hexadecimal representation of ASCII values + if result != expected { + t.Errorf("Expected '%s', but got '%s'", expected, result) + } +} + +func TestHumanizeBytes(t *testing.T) { + tests := []struct { + bytes int64 + expected string + }{ + {500, "500 B"}, + {1024, "1.00 KB"}, + {1536, "1.50 KB"}, + {1048576, "1.00 MB"}, + {1572864, "1.50 MB"}, + {1073741824, "1.00 GB"}, + {1610612736, "1.50 GB"}, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("%dBytes", test.bytes), func(t *testing.T) { + result := HumanizeBytes(test.bytes) + if result != test.expected { + t.Errorf("HumanizeBytes(%d) = %s; want %s", test.bytes, result, test.expected) + } + }) + } +}