Skip to content

Commit e41b50a

Browse files
Added main and test files for converting hexadecimal to binary and hexadecimal to decimal
1 parent 94689d8 commit e41b50a

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

conversion/hexadecimaltobinary.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Author: mapcrafter2048
3+
GitHub: https://github.com/mapcrafter2048
4+
*/
5+
6+
// This algorithm will convert any Hexadecimal number(0-9, A-F, a-f) to Binary number(0 or 1).
7+
// https://en.wikipedia.org/wiki/Hexadecimal
8+
// https://en.wikipedia.org/wiki/Binary_number
9+
// Function receives a Hexadecimal Number as string and returns the Binary number as string.
10+
// Supported Hexadecimal number range is 0 to 7FFFFFFFFFFFFFFF.
11+
12+
package conversion
13+
14+
import (
15+
"errors"
16+
"regexp"
17+
"strconv"
18+
"strings"
19+
)
20+
21+
var isValidHex = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString
22+
23+
// hexToBinary() function that will take Hexadecimal number as string,
24+
// and return its Binary equivalent as a string.
25+
func hexToBinary(hex string) (string, error) {
26+
// Trim any leading or trailing whitespace
27+
hex = strings.TrimSpace(hex)
28+
29+
// Check if the hexadecimal string is empty
30+
if hex == "" {
31+
return "", errors.New("input string is empty")
32+
}
33+
34+
// Check if the hexadecimal string is valid
35+
if !isValidHex(hex) {
36+
return "", errors.New("invalid hexadecimal string: " + hex)
37+
}
38+
39+
// Parse the hexadecimal string to an integer
40+
decimal, err := strconv.ParseInt(hex, 16, 64)
41+
if err != nil {
42+
return "", errors.New("failed to parse hexadecimal string " + hex + ": " + err.Error())
43+
}
44+
45+
// Convert the integer to a binary string
46+
binary := strconv.FormatInt(decimal, 2)
47+
return binary, nil
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package conversion
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHexToBinary(t *testing.T) {
8+
tests := []struct {
9+
hex string
10+
want string
11+
wantErr bool
12+
}{
13+
{"", "", true},
14+
{"G123", "", true},
15+
{"12XZ", "", true},
16+
{"1", "1", false},
17+
{"A", "1010", false},
18+
{"10", "10000", false},
19+
{"1A", "11010", false},
20+
{"aB", "10101011", false},
21+
{"0Ff", "11111111", false},
22+
{" 1A ", "11010", false},
23+
{"0001A", "11010", false},
24+
{"7FFFFFFFFFFFFFFF", "111111111111111111111111111111111111111111111111111111111111111", false},
25+
}
26+
27+
for _, tt := range tests {
28+
t.Run(tt.hex, func(t *testing.T) {
29+
got, err := hexToBinary(tt.hex)
30+
if (err != nil) != tt.wantErr {
31+
t.Errorf("hexToBinary(%q) error = %v, wantErr %v", tt.hex, err, tt.wantErr)
32+
return
33+
}
34+
if got != tt.want {
35+
t.Errorf("hexToBinary(%q) = %v, want %v", tt.hex, got, tt.want)
36+
}
37+
})
38+
}
39+
}
40+
41+
func BenchmarkHexToBinary(b *testing.B) {
42+
b.ReportAllocs()
43+
for i := 0; i < b.N; i++ {
44+
_, _ = hexToBinary("7FFFFFFFFFFFFFFF")
45+
}
46+
}

conversion/hexadecimaltodecimal.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Author: mapcrafter2048
3+
GitHub: https://github.com/mapcrafter2048
4+
*/
5+
6+
// This algorithm will convert any Hexadecimal number(0-9, A-F, a-f) to Decimal number(0-9).
7+
// https://en.wikipedia.org/wiki/Hexadecimal
8+
// https://en.wikipedia.org/wiki/Decimal
9+
// Function receives a Hexadecimal Number as string and returns the Decimal number as integer.
10+
// Supported Hexadecimal number range is 0 to 7FFFFFFFFFFFFFFF.
11+
12+
package conversion
13+
14+
import (
15+
"fmt"
16+
"regexp"
17+
"strconv"
18+
"strings"
19+
)
20+
21+
var isValidHexadecimal = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString
22+
23+
// hexToDecimal converts a hexadecimal string to a decimal integer.
24+
func hexToDecimal(hexStr string) (int64, error) {
25+
26+
hexStr = strings.TrimSpace(hexStr)
27+
28+
if len(hexStr) == 0 {
29+
return 0, fmt.Errorf("input string is empty")
30+
}
31+
32+
// Check if the string has a valid hexadecimal prefix
33+
if len(hexStr) > 2 && (hexStr[:2] == "0x" || hexStr[:2] == "0X") {
34+
hexStr = hexStr[2:]
35+
}
36+
37+
// Validate the hexadecimal string
38+
if !isValidHexadecimal(hexStr) {
39+
return 0, fmt.Errorf("invalid hexadecimal string")
40+
}
41+
42+
// Convert the hexadecimal string to a decimal integer
43+
decimalValue, err := strconv.ParseInt(hexStr, 16, 64)
44+
if err != nil {
45+
return 0, fmt.Errorf("invalid hexadecimal string: %v", err)
46+
}
47+
48+
return decimalValue, nil
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package conversion
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHexToDecimal(t *testing.T) {
8+
tests := []struct {
9+
hex string
10+
want int64
11+
wantErr bool
12+
}{
13+
{"", 0, true},
14+
{"G123", 0, true},
15+
{"123Z", 0, true},
16+
{"1", 1, false},
17+
{"A", 10, false},
18+
{"10", 16, false},
19+
{"1A", 26, false},
20+
{"aB", 171, false},
21+
{"0Ff", 255, false},
22+
{" 1A ", 26, false},
23+
{"0x1A", 26, false},
24+
{"0X1A", 26, false},
25+
{"1A", 26, false},
26+
{"7FFFFFFFFFFFFFFF", 9223372036854775807, false},
27+
{"0001A", 26, false},
28+
{"0000007F", 127, false},
29+
{"0", 0, false},
30+
{"0x0", 0, false},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.hex, func(t *testing.T) {
35+
got, err := hexToDecimal(tt.hex)
36+
if (err != nil) != tt.wantErr {
37+
t.Errorf("hexToDecimal(%q) error = %v, wantErr %v", tt.hex, err, tt.wantErr)
38+
return
39+
}
40+
if got != tt.want {
41+
t.Errorf("hexToDecimal(%q) = %v, want %v", tt.hex, got, tt.want)
42+
}
43+
})
44+
}
45+
}
46+
47+
func BenchmarkHexToDecimal(b *testing.B) {
48+
b.ReportAllocs()
49+
for i := 0; i < b.N; i++ {
50+
_, _ = hexToDecimal("7FFFFFFFFFFFFFFF")
51+
}
52+
}

0 commit comments

Comments
 (0)