encode BCD with right-padded F #66

This commit is contained in:
Enrique Zamudio
2018-04-17 14:16:09 -05:00
parent 51da1fe599
commit bc3c2b553b
@@ -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.