patternjavaModerate
Multiply two hexadecimal values
Viewed 0 times
valuestwomultiplyhexadecimal
Problem
I've written an algorithm which can multiply two hex values and return a hex as a result. What do you think about the below solution?
```
package com.datastructute.arraystring;
public class hexadecimal {
public static void main(String[] args) {
System.out.println(multiplyHex("AE08FE2111111111", "BF"));
System.out.println(multiplyHex("DF", "BC")); // A3C4
System.out.println(multiplyHex("ADF398", "BA48")); // 7e93e8f2c0
// Test Screnarios
System.out.println(multiplyHex(null, null));
System.out.println(multiplyHex(" ", " "));
System.out.println(multiplyHex("hyh", "hyhy"));
System.out.println(multiplyHex("abyh", "ashyhy"));
System.out.println(multiplyHex("-1-1-1", "-1-1-1"));
System.out.println(multiplyHex("AE08FE2111111111AE08FE2AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE21111111111111AE08FE2111111111AE08FE21111111111111AE08FE21111AE08FE211111111111111", "AE0AE08FE21111111118FE2111111111AE08FE2111111111"));
}
public static String multiplyHex(String str1, String str2) {
if(str1 == null || str2 == null)
return "Null values are not accepted.";
char[] hex1 = str1.toCharArray();
char[] hex2 = str2.toCharArray();
int[][] ArrHexMatrix;
int arrLength = hex1.length + hex2.length;
int arrIndexLength = hex1.length + hex2.length - 1;
int lines = hex2.length;
ArrHexMatrix = new int[hex2.length][arrLength];
int mod = 0;
int carry = 0;
int count = 0;
int index = 0;
for (int i = lines - 1; i >= 0; i--) {
carry = 0;
count = 0;
for (int j = hex1.length - 1; j >= 0; j--) {
try {
```
package com.datastructute.arraystring;
public class hexadecimal {
public static void main(String[] args) {
System.out.println(multiplyHex("AE08FE2111111111", "BF"));
System.out.println(multiplyHex("DF", "BC")); // A3C4
System.out.println(multiplyHex("ADF398", "BA48")); // 7e93e8f2c0
// Test Screnarios
System.out.println(multiplyHex(null, null));
System.out.println(multiplyHex(" ", " "));
System.out.println(multiplyHex("hyh", "hyhy"));
System.out.println(multiplyHex("abyh", "ashyhy"));
System.out.println(multiplyHex("-1-1-1", "-1-1-1"));
System.out.println(multiplyHex("AE08FE2111111111AE08FE2AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE2111111111AE08FE21111111111111AE08FE2111111111AE08FE21111111111111AE08FE21111AE08FE211111111111111", "AE0AE08FE21111111118FE2111111111AE08FE2111111111"));
}
public static String multiplyHex(String str1, String str2) {
if(str1 == null || str2 == null)
return "Null values are not accepted.";
char[] hex1 = str1.toCharArray();
char[] hex2 = str2.toCharArray();
int[][] ArrHexMatrix;
int arrLength = hex1.length + hex2.length;
int arrIndexLength = hex1.length + hex2.length - 1;
int lines = hex2.length;
ArrHexMatrix = new int[hex2.length][arrLength];
int mod = 0;
int carry = 0;
int count = 0;
int index = 0;
for (int i = lines - 1; i >= 0; i--) {
carry = 0;
count = 0;
for (int j = hex1.length - 1; j >= 0; j--) {
try {
Solution
Your
Using arrays:
Java has already built-in methods to do this conversion though, so all that is needed is really:
Hexadecimal values are often showed in lower-case, personally I like that better because it makes it easier to separate the characters (a-f) from the digits (0-9). If you still want to use upper-case characters though, use this:
Don't throw
Learn to use JUnit to do the testing.
getInt and getChar methods are very long and cumbersome, they can be replaced by this:Using arrays:
private char[] values = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char getInt(int i) {
return values[i];
}Java has already built-in methods to do this conversion though, so all that is needed is really:
int getInt(char chr) {
return Character.digit(chr, 16);
}
char getChar(int val) {
return Character.forDigit(val, 16);
}Hexadecimal values are often showed in lower-case, personally I like that better because it makes it easier to separate the characters (a-f) from the digits (0-9). If you still want to use upper-case characters though, use this:
return Character.toUpperCase(Character.forDigit(val, 16));private static char getChar(int val) throws Exception {Don't throw
Exception, use IllegalArgumentException instead. IllegalArgumentException is a RuntimeException and therefore does not need to be caught (or declared in the throws declaration), but you are still free to do so if you would like. (Although I would not recommend it)Learn to use JUnit to do the testing.
import static org.junit.Assert.*;
public class MultiplyHexTest {
@Test
public void testSomething() {
assertEquals("A3C4", multiplyHex("DF", "BC"));
}
@Test(expected = IllegalArgumentException.class)
public void testFailure() {
multiplyHex(" ", " ");
}
}ArrHexMatrix, being a name for a variable, should start with a lowercase character according to the Java naming conventions.Code Snippets
private char[] values = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char getInt(int i) {
return values[i];
}int getInt(char chr) {
return Character.digit(chr, 16);
}
char getChar(int val) {
return Character.forDigit(val, 16);
}return Character.toUpperCase(Character.forDigit(val, 16));private static char getChar(int val) throws Exception {import static org.junit.Assert.*;
public class MultiplyHexTest {
@Test
public void testSomething() {
assertEquals("A3C4", multiplyHex("DF", "BC"));
}
@Test(expected = IllegalArgumentException.class)
public void testFailure() {
multiplyHex(" ", " ");
}
}Context
StackExchange Code Review Q#52084, answer score: 12
Revisions (0)
No revisions yet.