Skip to content
38 changes: 36 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,19 @@ public static String trim (String s) {
public static String zeropad(String s, int len) throws ISOException {
return padleft(s, len, '0');
}


public static boolean isHexadecimal(byte[] bytes) {
for (byte b : bytes) {

Choose a reason for hiding this comment

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

porque tenes 2 funciones "isHexadecimal" ?

En todo caso, podes hacer que esta que recibe byte[] le llame a la anterior. De otra manera, estarias teniendo dos implementaciones de lo mismo.

Copy link
Owner Author

Choose a reason for hiding this comment

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

se me ocurrio hacerlo asi, pero voy a modificarlos sin problemas.

int unsignedByte = b & 0xFF;
if (!((unsignedByte >= 0x30 && unsignedByte <= 0x39) || // Dígitos 0-9
(unsignedByte >= 0x41 && unsignedByte <= 0x46) || // Letras A-F
(unsignedByte >= 0x61 && unsignedByte <= 0x66))) { // Letras a-f
return false;
}
}
return true;
}

/**
* zeropads a long without throwing an ISOException (performs modulus operation)
*
Expand Down Expand Up @@ -680,8 +696,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;
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("1111");
}, "Not hex");

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


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