From bc3c2b553bdd4d675f860aba0704a39a113347c2 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Tue, 17 Apr 2018 14:16:09 -0500 Subject: [PATCH] encode BCD with right-padded F #66 --- src/main/java/com/solab/iso8583/util/Bcd.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/solab/iso8583/util/Bcd.java b/src/main/java/com/solab/iso8583/util/Bcd.java index b0103cc..a9c2cc1 100644 --- a/src/main/java/com/solab/iso8583/util/Bcd.java +++ b/src/main/java/com/solab/iso8583/util/Bcd.java @@ -71,6 +71,30 @@ public final class Bcd { } } + /** Encode the value as BCD and put it in the buffer. The buffer must be big enough + * to store the digits in the original value (half the length of the string). + * If the value contains an odd number of digits, the last one is stored in + * its own byte at the end, padded with an F nibble. */ + public static void encodeRightPadded(String value, byte[] buf) { + int bufpos = 0; + int charpos = 0; + int limit = value.length(); + if (limit % 2 == 1) { + limit--; + } + //encode the rest of the string + while (charpos < limit) { + buf[bufpos] = (byte)(((value.charAt(charpos) - 48) << 4) + | (value.charAt(charpos + 1) - 48)); + charpos += 2; + bufpos++; + } + if (value.length() % 2 == 1) { + buf[bufpos] = (byte)(((value.charAt(limit) - 48) << 4) + | 0xf); + } + } + /** Decodes a BCD-encoded number as a BigInteger. * @param buf The byte buffer containing the BCD data. * @param pos The starting position in the buffer.