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

五迷三道 提交于 2019-11-30 06:02:45

问题


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.


回答1:


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.




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



回答3:


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.



来源:https://stackoverflow.com/questions/4337912/how-to-convert-unpacked-decimal-back-to-comp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!