How to convert unpacked decimal point back to COMP-3?

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

I hope the stack overflow can help me with this issue.

I managed to convert COMP-3 to decimal. I need your help converting the decompressed decimal place back to COMP-3 in any high-level programming language, but preferably in Java or C # .net.

+3
source share
3 answers

In the packed decimal value, -123 is represented as X'123d '(the last nyble c, d or f is a sign). One of the easiest ways to handle a packed decimal is to simply convert the bytes to a hexadecimal string (or vice versa if necessary), then use the usual string manipulation. This may not be the most effective, but it is easy to implement.

So, to convert an integer (value) to a packed decimal approximately (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(); 

Below is an example of code to convert to / from comp3. To get a packed decimal number from an array of bytes, see the getMainframePackedDecimal Method at 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 the packed decimal, see setField at http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Types/TypePackedDecimal.java?revision= 3 & view = markup

both procedures take an array of bytes, an initial position and a field length.

There are other examples of this on the Internet (JRanch, I think, has code to do the conversion), do a little search.

+3
source

Converting zoned decimal to comp-3 is pretty simple - flip the nibbles of the low byte and separate a large chunk of all the other bytes.

Consider the number 12345 - in the packed decimal notation, which will be x'12345C 'or x'12345F' (both C and F are +, AB and D are -). When you turned it into a zoned decimal, you flipped the lower nibble and inserted "F" in the high piece between each digit. Including it in x'F1F2F3F4C5 '.

To convert it back, you just cancel the process. Using java, it will look like this:

 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); 
+2
source

When I messed up COMP-3 in the past with Java, I ended up writing a method for reading in bytes and converting them to a number. I don’t think I have ever written COMP-3, but I believe that I would use the same logic in the opposite direction.

0
source

Source: https://habr.com/ru/post/1393829/


All Articles