开发者

How to convert unpacked decimal back to COMP-3? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question

I had a开发者_开发知识库sked 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.


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.


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜