-1

I am working on java code, I need to convert int data to packed decimal in Java. Because packed decimal is an old format (COBOL). The solutions that i found dealt with converting int to packed decimal, but not float. I have two variables, the length and how many numbers come after decimal point must be stored as packed decimal.

for example: length = 6(in half a byte)

That means that total length=3 bytes+1/5 byte+1/5 byte(To get even number of bytes) Number of nibbles after decimal point stored = 2

(Unsigned) 12345.67 >> 1234567F

in case Number of nibbles=4

(Unsigned) 12345.67 >> 3456700F

Any code will be highly appreciated.

alan
  • 48
  • 1
  • 13
  • 4
    Unfortunately, we cannot simply just provide code for you. Please post your desired results (an example of an output you would like) and any attempts you have made. – artemis Jun 07 '19 at 15:17
  • I just posted more details – alan Jun 26 '19 at 17:20
  • The answer I posted converts to packed decimal. I don't understand how it doesn't work. – artemis Jun 26 '19 at 18:16
  • Well, The code that you posted converts long to packed decimal, not including the float and big decimal. Then the code has a stable number of digit 5, that is not how packed decimal works, It is mostly about shifting values to the left and right based on the length and number of digits after the decimal point. – alan Jun 27 '19 at 14:54

1 Answers1

1

Here is an example from this post provided by @GilbertLeBlanc that should fix be a solution to your problem.

public class PackedDecimal {

    public static byte[] format(long number, int bytes) {
        byte[] b = new byte[bytes];

        final byte minusSign = 0x0D; // Minus
        final byte noSign = 0x0F; // Unsigned

        String s = Long.toString(number);
        int length = s.length();
        boolean isNegative = false;

        if (s.charAt(0) == '-') {
            isNegative = true;
            s = s.substring(1);
            length--;
        }

        int extraBytes = length - bytes + 1;

        if (extraBytes < 0) {
            // Pad extra byte positions with zero
            for (int i = 0; i < -extraBytes; i++) {
                b[i] = 0x00;
            }
        } else if (extraBytes > 0) {
            // Truncate the high order digits of the number to fit
            s = s.substring(extraBytes);
            length -= extraBytes;
            extraBytes = 0;
        }

        // Translate the string digits into bytes
        for (int i = 0; i < length; i++) {
            String digit = s.substring(i, i + 1);
            b[i - extraBytes] = Byte.valueOf(digit);
        }

        // Add the sign byte
        if (isNegative) {
            b[bytes - 1] = minusSign;
        } else {
            b[bytes - 1] = noSign;
        }

        return b;
    }

    public static void main(String[] args) {
        long number = -456L;
        byte[] b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 0L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 5823L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 123456L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));
    }

    public static String byteToString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            sb.append("0x");
            sb.append(Integer.toHexString((int) b[i]).toUpperCase());
            sb.append(" ");
        }
        return sb.toString();
    }

}

The results from the test in the main method are below:

Number: -456, packed: 0x0 0x4 0x5 0x6 0xD 
Number: 0, packed: 0x0 0x0 0x0 0x0 0xF 
Number: 5823, packed: 0x5 0x8 0x2 0x3 0xF 
Number: 123456, packed: 0x3 0x4 0x5 0x6 0xF
artemis
  • 6,857
  • 11
  • 46
  • 99
  • Is this code actually doing **packed** decimal? I don't see any packing (i.e. two digits in each byte) but I haven't entirely scrutinized it so maybe it does. – k314159 Aug 22 '22 at 16:07
  • I haven't looked at this solution in many years -- unfortunately, I can't answer with certainty @k314159 – artemis Aug 22 '22 at 20:56