|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectname.benjaminjwhite.zdecimal.PackDec
public class PackDec
Utility class to handle IBM Z/Series 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
Z/series 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:
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
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).
Z/Series, 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.
Constructor Summary | |
---|---|
PackDec()
|
Method Summary | |
---|---|
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 |
---|
public PackDec()
Method Detail |
---|
public static java.lang.String bytesToHex(byte[] bytes)
bytes
- array of bytes
public static byte[] longToPack(long lnum)
lnum
- long number to convert
public static void longToPack(long lnum, byte[] bytearray, int offset, int len) throws DecimalOverflowException
lnum
- Number to be convertedbytearray
- Contains resultoffset
- Location in arraylen
- Number of bytes of result
DecimalOverflowException
- If result is larger than result lengthpublic static long packToLong(byte[] pknum) throws DataException, FixedPointDivideException
pknum
- byte array of 1 to 16
DataException
- input is not packed decimal format
FixedPointDivideException
- number would not fit in longpublic static long packToLong(byte[] bytearray, int offset, int len) throws DataException, FixedPointDivideException
bytearray
- contains packed numberoffset
- into byte arraylen
- length of packed field
DataException
FixedPointDivideException
public static java.lang.String packToString(byte[] pknum) throws DataException
pknum
- byte array containing packed number, 1 to 16 long
DataException
- input is not packed decimal formatpublic static java.lang.String packToString(byte[] bytearray, int offset, int len) throws DataException, FixedPointDivideException
bytearray
- that contains a packed number and possibly other dataoffset
- to the packed nubmer to be convertedlen
- number of bytes in length of the packed number
DataException
- selected array is not in packed decimal format
FixedPointDivideException
public static byte[] stringToPack(java.lang.String str) throws DataException, DecimalOverflowException
str
- String of number to convert
DataException
- Invalid characters in input string
DecimalOverflowException
- Too many digits in input stringpublic static void stringToPack(java.lang.String str, byte[] bytearray, int offset, int len) throws DecimalOverflowException, DataException
str
- Number to be convertedbytearray
- Contains resultoffset
- Location in arraylen
- Number of bytes of result
DecimalOverflowException
- If packed number is too big to fit in the target length.
DataException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |