diff --git a/src/main/java/com/solab/iso8583/IsoValue.java b/src/main/java/com/solab/iso8583/IsoValue.java index 4145706..e3ac609 100644 --- a/src/main/java/com/solab/iso8583/IsoValue.java +++ b/src/main/java/com/solab/iso8583/IsoValue.java @@ -76,7 +76,11 @@ public class IsoValue implements Cloneable { } } else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) { if (custom == null) { - length = ((byte[])value).length; + if (value instanceof byte[]) { + length = ((byte[])value).length; + } else { + length = value.toString().length() / 2 + (value.toString().length() % 2); + } } else { //TODO special encoder, NO strings String enc = custom.encodeField(value); @@ -177,9 +181,17 @@ public class IsoValue implements Cloneable { } else if (value instanceof Date) { return type.format((Date)value); } else if (type == IsoType.BINARY) { - return type.format(encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value), length * 2); + if (value instanceof byte[]) { + return type.format(encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value), length * 2); + } else { + return type.format(encoder == null ? value.toString() : encoder.encodeField(value), length); + } } else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) { - return encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value); + if (value instanceof byte[]) { + return encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value); + } else { + return encoder == null ? value.toString() : encoder.encodeField(value); + } } return encoder == null ? value.toString() : encoder.encodeField(value); } @@ -273,7 +285,11 @@ public class IsoValue implements Cloneable { } } if (binary && (type == IsoType.BINARY || type == IsoType.LLBIN || type == IsoType.LLLBIN)) { - outs.write((byte[])value); + if (value instanceof byte[]) { + outs.write((byte[])value); + } else { + outs.write(HexCodec.hexDecode(value.toString())); + } } else { //Just write the value as text outs.write(toString().getBytes()); diff --git a/src/main/resources/com/solab/iso8583/parse/j8583.dtd b/src/main/resources/com/solab/iso8583/parse/j8583.dtd index 79bf080..b886a63 100644 --- a/src/main/resources/com/solab/iso8583/parse/j8583.dtd +++ b/src/main/resources/com/solab/iso8583/parse/j8583.dtd @@ -12,4 +12,4 @@ - + diff --git a/src/test/java/j8583/TestBinaries.java b/src/test/java/j8583/TestBinaries.java new file mode 100644 index 0000000..e4d3b99 --- /dev/null +++ b/src/test/java/j8583/TestBinaries.java @@ -0,0 +1,45 @@ +package j8583; + +import java.io.IOException; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.solab.iso8583.IsoMessage; +import com.solab.iso8583.MessageFactory; +import com.solab.iso8583.util.HexCodec; + +/** Test binary message encoding and binary fields. */ +public class TestBinaries { + + private MessageFactory mfactAscii = new MessageFactory(); + private MessageFactory mfactBin = new MessageFactory(); + + @Before + public void setup() throws IOException { + mfactAscii.setConfigPath("config.xml"); + mfactBin.setConfigPath("config.xml"); + mfactAscii.setAssignDate(true); + mfactBin.setAssignDate(true); + mfactBin.setUseBinaryMessages(true); + } + + @Test + public void testMessages() { + //Create a message with both factories + IsoMessage ascii = mfactAscii.newMessage(0x600); + IsoMessage bin = mfactBin.newMessage(0x600); + //HEXencode the binary message, headers should be similar to the ASCII version + String hexBin = HexCodec.hexEncode(bin.writeData()); + String hexAscii = new String(ascii.writeData()); + System.out.printf("bin: %s%nascii: %s%n", hexBin, hexAscii); + Assert.assertEquals("0600", hexBin.substring(0, 4)); + //Should be the same up to the trace (first 64 chars) + Assert.assertEquals(hexAscii.substring(0, 64), hexBin.substring(0, 64)); + //Parse both messages + //Compare values, should be the same + } + +} diff --git a/src/test/resources/config.xml b/src/test/resources/config.xml index 312e1fe..a198a0a 100644 --- a/src/test/resources/config.xml +++ b/src/test/resources/config.xml @@ -37,6 +37,15 @@ ...and yet another fixed field. + + @@ -88,4 +97,15 @@ + + + + + + + + + + +