From c1bed18cb1cafc7afc81217c4f3e9a7874144f05 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Tue, 17 Apr 2018 14:21:13 -0500 Subject: [PATCH] decode right padded to long #66 --- src/main/java/com/solab/iso8583/util/Bcd.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/com/solab/iso8583/util/Bcd.java b/src/main/java/com/solab/iso8583/util/Bcd.java index a9c2cc1..bb9f815 100644 --- a/src/main/java/com/solab/iso8583/util/Bcd.java +++ b/src/main/java/com/solab/iso8583/util/Bcd.java @@ -51,6 +51,28 @@ public final class Bcd { return l; } + public static long decodeRightPaddedToLong(byte[] buf, int pos, int length) + throws IndexOutOfBoundsException { + if (length > 18) { + throw new IndexOutOfBoundsException("Buffer too big to decode as long"); + } + long l = 0; + long power = 1L; + int end = pos + (length / 2) + (length % 2) - 1; + if ((buf[end] & 0xf) == 0xf) { + l += (buf[end] & 0xf0) >> 4; + power *= 10L; + end--; + } + for (int i = end; i >= pos; i--) { + l += (buf[i] & 0x0f) * power; + power *= 10L; + l += ((buf[i] & 0xf0) >> 4) * power; + power *= 10L; + } + return l; + } + /** 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). */ public static void encode(String value, byte[] buf) {