From b0679fafdeb4c6909b5c16b2698fb78a6902bfa2 Mon Sep 17 00:00:00 2001 From: Kent Louise Tonino Date: Wed, 8 May 2024 19:38:58 +0800 Subject: [PATCH] Fix Binary to Hexadecimal Does Not Support Zeroes Only Issue --- src/services/binary/get_binary_to_hexadecimal.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/services/binary/get_binary_to_hexadecimal.go b/src/services/binary/get_binary_to_hexadecimal.go index 75a31e8..ee0d11f 100644 --- a/src/services/binary/get_binary_to_hexadecimal.go +++ b/src/services/binary/get_binary_to_hexadecimal.go @@ -1,14 +1,19 @@ package binary -import "strconv" +import ( + "strconv" +) -// Converts binary number to hexadecial. func getBinaryToHexadecimal(binary int64) string { hexadecimal := "" reversedBinaryArray := getReversedBinaryArray(binary) arrayOfHexadecimal := []int64{} groupSum := int64(0) + if binary == 0 { + return "0" + } + for i := 0; i < len(reversedBinaryArray); i++ { numWeight := i % 4 isLastIndex := i == (len(reversedBinaryArray) - 1)