5

I had asked a question about converting COMP fields, for which I did not get any answer.

I hope stack-overflow can help me on this question.

I succeeded in converting COMP-3 to decimal. I need your help in converting the unpacked decimal back to COMP-3, in any high level programming language, but preferably in Java or c#.net.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
Krishna Kumar N
  • 137
  • 2
  • 13
  • Ummm... If you can successfully convert COMP-3 to decimal, what is stopping you from just reversing the process for a decimal to COMP-3 conversion? – NealB Dec 02 '10 at 18:16
  • 1
    Why is this difficult? Take the lower 4 bits of unsigned digits, put them together with shifts and bitwise operations, and tack the appropriate sign indicator on the end. Take a stab at it, then if you have problems come back here and ask about them. Or write it out in ASCII or EBCDIC and write the translator in COBOL. – David Thornley Dec 02 '10 at 18:39
  • @David, can you put some more light on the part of writing a COBOL translator?? Cause my code will be in .NET and if possible can you give me the details of converting the output into EBCDIC format? – Krishna Kumar N Dec 06 '10 at 07:44
  • I really don't know what you're trying to do. Why do you need it back in COMP-3? If it's to interface with a COBOL program, COBOL can read text as well as COMP-3, and a COBOL program could do any necessary conversion. EBCDIC is an 8-bit character code, and a near-direct ASCII to EBCDIC translation should be easy (I don't know that EBCDIC has all the more obscure ASCII characters). If nothing else, prepare output in ASCII, then do a byte-by-byte conversion. – David Thornley Dec 06 '10 at 14:53

3 Answers3

3

In packed decimal -123 is represented as X'123d' (the last nyble c,d or f being the sign). One of the simplest ways to handle packed decimal is to simply convert the bytes to a hex string (or vice versa as required) then use normal string manipulation. This may not be the most efficient but it is easy to implement.

So to convert a Integer (value) to packed decimal is roughly (note: I have not tested the code)

String sign = "c";
if (value < 0) {
    sign = "d";
    value = -1 * value;
}
String val = value + "d"

byte[] comp3Bytes = new BigInteger(val, 16).toByteArray();

Following are some example code for converting to/from comp3 To retrieve a packed decimal from an array of bytes see method getMainframePackedDecimal in http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Common/Conversion.java?revision=3&view=markup

and to set a packed decimal see setField in http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Types/TypePackedDecimal.java?revision=3&view=markup

both routines take an array of bytes, a start position and either a length of a field position.

There are other examples of doing this on the web (JRanch I think has code for doing the conversion as well), do a bit of googling.

Shekhar
  • 11,438
  • 36
  • 130
  • 186
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
2

Converting zoned decimal to comp-3 is quite easy -- flip the nibbles of the low byte and strip off the high nibble of all other bytes.

Consider the number 12345 -- in packed decimal notation, that would be a x'12345C' or x'12345F' (both C and F are +, A B and D are -). When you converted it to zoned decimal, you flipped the low nibble and inserted a "F" in the high nibble between each digit. Turning it into x'F1F2F3F4C5'.

To convert it back, you just reverse the process. Using java, that would look like:

byte[] myDecimal = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5 };
byte[] myPacked = new byte[3];

//Even low nibble moved to high nibble and merged with odd low nibble
myPacked[0] = ((myDecimal[0] & 0b00001111)  << 4)) | (myDecimal[1] & 0b00001111);
myPacked[1] = ((myDecimal[2] & 0b00001111)  << 4)) | (myDecimal[3] & 0b00001111);

//Last byte gets filpped for sign
myPacked[2] = ((myDecimal[5] & 0b00001111)  << 4)) | (myDecimal[4] & 0b00001111);
Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
0

When I have messed with COMP-3 in the past with Java I ended up writing a method to read in the bytes and convert them to a number. I don't think I ever had to write COMP-3 out, but I assume I would use the same logic in reverse.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • @Krishna: I don't have the code handy at the moment and might have thrown it out when it wasn't needed anymore. I think I just read the definitions for comp-3 and determined how to arrange the nibbles. – jzd Dec 02 '10 at 18:11
  • COMP-3 is hexadecimal representation and the sign bit is to the end of the representation. it represents two digits as a single byte. positive is identified by 0x0C or 0x0F at the end. Negative is identified by 0x0D at the end of the representation. – Krishna Kumar N Dec 02 '10 at 19:31
  • COMP-3 is a *decimal* representation and the sign *nibble* is at the end. Otherwise correct. – user207421 Dec 03 '10 at 00:59