Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions jpos/src/main/java/org/jpos/iso/ISOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.regex.Pattern;
import java.math.BigInteger;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
* various functions needed to pack/unpack ISO-8583 fields
Expand Down Expand Up @@ -177,7 +181,7 @@ public static String trim (String s) {
public static String zeropad(String s, int len) throws ISOException {
return padleft(s, len, '0');
}

/**
* zeropads a long without throwing an ISOException (performs modulus operation)
*
Expand Down Expand Up @@ -680,8 +684,26 @@ public static byte[] hex2byte (byte[] b, int offset, int len) {
* @return byte array
*/
public static byte[] hex2byte (String s) {
return hex2byte (s, CHARSET);

if (s.isEmpty() || s.length() % 2 != 0) //verificar si el string es vacio
{
throw new IllegalArgumentException("The string is empty or null, invalid hexadecimal");
}
// Verificar si cada carácter es un dígito hexadecimal válido
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!isDigitHexadecimal(c)) {
throw new IllegalArgumentException("The string is not hexadecimal");
}
}
return hex2byte(s, CHARSET);

}

public static boolean isDigitHexadecimal(char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}

/**
* Converts a hex string into a byte array
* @param s source string (with Hex representation)
Expand Down
26 changes: 25 additions & 1 deletion jpos/src/test/java/org/jpos/iso/ISOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.assertArrayEquals;

import java.math.BigDecimal;
import java.math.MathContext;
Expand All @@ -43,13 +44,36 @@
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Assertions;
import org.fest.assertions.Assert;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

este import no es necesario y tampoco eliminar

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entonces la quito nomas?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no modifiques las lineas que no necesitas. Es buena practicar modificar el mínimo posible.

import org.junit.jupiter.api.Test;
import org.junit.Assert;
import java.util.Arrays;

public class ISOUtilTest {
final String lineSep = System.getProperty("line.separator");



@Test
public void testHex2byte_ValidInput() {
byte[] result = ISOUtil.hex2byte("1A2B3C");
byte [] expected = {0x1A, 0x2B, 0x3C};
assertArrayEquals(expected, result);
}

@Test
public void testHex2byte_InvalidInput_ThrowsExceptionWithMessage() {

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
ISOUtil.hex2byte("1A2B3C");
}, "Not hex");

assertEquals("Not hex", exception.getMessage());
}


@Test
public void testAsciiToEbcdic() throws Throwable {
byte[] a = new byte[0];
Expand Down