Work on #9 - Numeric codecs for LLBIN fields

This commit is contained in:
Enrique Zamudio
2013-05-07 13:44:27 -05:00
parent 0b50ae3f1d
commit 449e321645
2 changed files with 77 additions and 0 deletions
@@ -0,0 +1,40 @@
package com.solab.iso8583.codecs;
import com.solab.iso8583.CustomBinaryField;
import com.solab.iso8583.util.Bcd;
import java.math.BigInteger;
/**
* A custom field encoder/decoder to be used with LLBIN/LLLBIN fields
* that contain BigIntegers in BCD encoding.
*
* @author Enrique Zamudio
* Date: 07/05/13 13:02
*/
public class BigIntBcdCodec implements CustomBinaryField<BigInteger> {
@Override
public BigInteger decodeBinaryField(byte[] value, int pos, int len) {
return Bcd.decodeToBigInteger(value, pos, len*2);
}
@Override
public byte[] encodeBinaryField(BigInteger value) {
final String s = value.toString(10);
final byte[] buf = new byte[s.length() / 2 + s.length() % 2];
Bcd.encode(s, buf);
return buf;
}
@Override
public BigInteger decodeField(String value) {
return new BigInteger(value, 10);
}
@Override
public String encodeField(BigInteger value) {
return value.toString(10);
}
}
@@ -0,0 +1,37 @@
package com.solab.iso8583.codecs;
import com.solab.iso8583.CustomBinaryField;
import com.solab.iso8583.util.Bcd;
/**
* A custom field encoder/decoder to be used with LLBIN/LLLBIN fields
* that contain Longs in BCD encoding.
*
* @author Enrique Zamudio
* Date: 07/05/13 13:02
*/
public class LongBcdCodec implements CustomBinaryField<Long> {
@Override
public Long decodeBinaryField(byte[] value, int pos, int length) {
return Bcd.decodeToLong(value, pos, length*2);
}
@Override
public byte[] encodeBinaryField(Long value) {
final String s = Long.toString(value, 10);
final byte[] buf = new byte[s.length() / 2 + s.length() % 2];
Bcd.encode(s, buf);
return buf;
}
@Override
public Long decodeField(String value) {
return Long.parseLong(value, 10);
}
@Override
public String encodeField(Long value) {
return value.toString();
}
}