Class PackDec


  • public class PackDec
    extends java.lang.Object
    Utility class to handle IBM System Z packed decimal data in Java. All packed number operands must be valid format with valid sign. The longest packed decimal number handled is 31 digits or 16 byted. Java long will not hold the maximum packed decimal number. java.math.bigdecimal can be used to handle larger numbers than will fit in java long. "Decimal packed" format is a method of representing numbers in the IBM System Z computers. It was also present in the 360, 370 and 390 series. Decimal digits are stored as 4 bits, 0 through 9, two digits per byte. The last four bits of a number are reserved for a sign. Positive are binary 1010, 1100 1110 and 1111. Negative is 1011 and 1101. For example the number -354 woud be stored as 0x354d, 7251 would be stored 0x07251c. COBOL is a popular mainframe language that has a number format with a "USAGE" of COMPUTATIONAL-3 or CCOMP-3. COMP-3 is stored as packed decimal. An example:
       01 PART-NUMBER.
          05  PART-NAME                   PIC X(20).
          05  PART-NUMBER                 PIC 9(5) USAGE IS COMP-3.
          05  PART-COST                   PIC 9(5)V99 USAGE IS COMP-3.
          05  FILLER                      PIC X(10).
     
    The PART-NUMBER would be stored in memory as packed decimal and occupy 3 bytes. The PART-COST would use 4 bytes. The implied decimal does not reserve any storage
    Sysem Z, 360, 370 and 390 is a trademark of IBM Corporation
    Thanks for optimization suggestions: comp.lang.java.programmer
    Esmond Pitt
    "Barak Shilo" for alert to decimal overflow bug in PackDec.
    satguru prasad srivastava for discovering input alteration bug.
    Version:
    5
    Author:
    zdecimal [ at ] benjaminjwhite.name
    See Also:
    IBM.COM, Z Decimal for java project home page
    • Constructor Summary

      Constructors 
      Constructor Description
      PackDec()  
    • Method Summary

      Modifier and Type Method Description
      static java.lang.String bytesToHex​(byte[] bytes)
      Hex values of Byte array
      static byte[] longToPack​(long lnum)
      Convert "long" to byte array 16 long of packed decimal number
      static void longToPack​(long lnum, byte[] bytearray, int offset, int len)
      Convenience method to convert a long to packed decimal.
      static long packToLong​(byte[] pknum)
      Convert a byte array containing a packed dicimal number to "long"
      static long packToLong​(byte[] bytearray, int offset, int len)
      Selects a packed decimal number from a byte array, then converts to a Long value of the number.
      static java.lang.String packToString​(byte[] pknum)
      Convert a byte array containing a packed dicimal number to String value
      static java.lang.String packToString​(byte[] bytearray, int offset, int len)
      Selects a packed decimal number from a byte array, then converts to a String value of the number.
      static byte[] stringToPack​(java.lang.String str)
      Converts String to packed decimal number.
      static void stringToPack​(java.lang.String str, byte[] bytearray, int offset, int len)
      Convenience method to convert a String to packed decimal.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PackDec

        public PackDec()
    • Method Detail

      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
        Hex values of Byte array
        Parameters:
        bytes - array of bytes
        Returns:
        Hex string of input
      • longToPack

        public static byte[] longToPack​(long lnum)
        Convert "long" to byte array 16 long of packed decimal number
        Parameters:
        lnum - long number to convert
        Returns:
        byte array 16 long
      • longToPack

        public static void longToPack​(long lnum,
                                      byte[] bytearray,
                                      int offset,
                                      int len)
                               throws DecimalOverflowException
        Convenience method to convert a long to packed decimal. The packed number is stored in an existing array.
        Parameters:
        lnum - Number to be converted
        bytearray - Contains result
        offset - Location in array
        len - Number of bytes of result
        Throws:
        DecimalOverflowException - If result is larger than result length
      • packToLong

        public static long packToLong​(byte[] bytearray,
                                      int offset,
                                      int len)
                               throws DataException,
                                      FixedPointDivideException
        Selects a packed decimal number from a byte array, then converts to a Long value of the number.
        Parameters:
        bytearray - contains packed number
        offset - into byte array
        len - length of packed field
        Returns:
        Long value of byte array of packed decimal
        Throws:
        DataException
        FixedPointDivideException
      • packToString

        public static java.lang.String packToString​(byte[] pknum)
                                             throws DataException
        Convert a byte array containing a packed dicimal number to String value
        Parameters:
        pknum - byte array containing packed number, 1 to 16 long
        Returns:
        String of packed number. First byte is sign
        Throws:
        DataException - input is not packed decimal format
      • packToString

        public static java.lang.String packToString​(byte[] bytearray,
                                                    int offset,
                                                    int len)
                                             throws DataException,
                                                    FixedPointDivideException
        Selects a packed decimal number from a byte array, then converts to a String value of the number.
        Parameters:
        bytearray - that contains a packed number and possibly other data
        offset - to the packed nubmer to be converted
        len - number of bytes in length of the packed number
        Returns:
        String value of packed number. First character is sign
        Throws:
        DataException - selected array is not in packed decimal format
        FixedPointDivideException
      • stringToPack

        public static byte[] stringToPack​(java.lang.String str)
                                   throws DataException,
                                          DecimalOverflowException
        Converts String to packed decimal number. Decimal points, commas and spaces are ignored. Sign character is processed. Avoid multiple signs. Characters other than digits are invalid and will cause DataException. Comma, blank, period, dollar sign and plus are ignored. Scaling and exponents are not valid.
        Parameters:
        str - String of number to convert
        Returns:
        byte array of packed decimal, 16 long
        Throws:
        DataException - Invalid characters in input string
        DecimalOverflowException - Too many digits in input string
      • stringToPack

        public static void stringToPack​(java.lang.String str,
                                        byte[] bytearray,
                                        int offset,
                                        int len)
                                 throws DecimalOverflowException,
                                        DataException
        Convenience method to convert a String to packed decimal. The packed number is stored in an existing array.
        Parameters:
        str - Number to be converted
        bytearray - Contains result
        offset - Location in array
        len - Number of bytes of result
        Throws:
        DecimalOverflowException - If packed number is too big to fit in the target length.
        DataException