Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Convert String to packed decimal
    primarykey
    data
    text
    <p>new here!</p> <p>Situation: I'm working on a project which needs to communicate with an AS/400 server. My task is to basically handle the requests which will be sent to the AS/400 server. To do this, all the user input should be in EDCDIC bytes.</p> <p>Problem:<br> I have managed to convert packed decimals to String with the code below, found <a href="http://www.coderanch.com/t/411222/java/java/Packed-decimal-conversion-Java" rel="nofollow">on this forum</a>:</p> <pre><code>public class PackedDecimal { public static long parse(byte[] pdIn) throws Exception { // Convert packed decimal to long final int PlusSign = 0x0C; // Plus sign final int MinusSign = 0x0D; // Minus final int NoSign = 0x0F; // Unsigned final int DropHO = 0xFF; // AND mask to drop HO sign bits final int GetLO = 0x0F; // Get only LO digit long val = 0; // Value to return for (int i = 0; i &lt; pdIn.length; i++) { int aByte = pdIn[i] &amp; DropHO; // Get next 2 digits &amp; drop sign bits if (i == pdIn.length - 1) { // last digit? int digit = aByte &gt;&gt; 4; // First get digit val = val * 10 + digit; // System.out.println("digit=" + digit + ", val=" + val); int sign = aByte &amp; GetLO; // now get sign if (sign == MinusSign) val = -val; else { // Do we care if there is an invalid sign? if (sign != PlusSign &amp;&amp; sign != NoSign) throw new Exception("OC7"); } } else { int digit = aByte &gt;&gt; 4; // HO first val = val * 10 + digit; // System.out.println("digit=" + digit + ", val=" + val); digit = aByte &amp; GetLO; // now LO val = val * 10 + digit; // System.out.println("digit=" + digit + ", val=" + val); } } return val; } // end parse() // Test the above public static void main(String[] args) throws Exception { byte[] pd = new byte[] { 0x19, 0x2C }; // 192 System.out.println(PackedDecimal.parse(pd)); pd = new byte[] { (byte) 0x98, 0x44, 0x32, 0x3D }; // -9844323 System.out.println(PackedDecimal.parse(pd)); pd = new byte[] { (byte) 0x98, 0x44, 0x32 }; // invalid sign System.out.println(PackedDecimal.parse(pd)); } } </code></pre> <p>My problem now is I have to convert these String values again to EBCDIC bytes so that the AS/400 server would understand it. I'm planning to do something like constructing a request (raw bytes) using the format specified in the Silverlake documentation. Once the request is built, I plan to manually change values inside that request using a POJO which stores my request (with setters and getters) so I could just go like <code>request.setField1("Stuff".getBytes(Charset.forName("Cp1047")))</code>.</p> <p>I don't have that much experience with bits, bytes and nibbles. I hope someone could help me out.</p> <p>In our code, there's a packed decimal we found which consists of 5 bytes. It goes something like = {00 00 00 00 0F}. I convert this using the method I got from the code above and the value I got was 0. Now, I would like to convert this 0 back to its original form with its original byte size 5.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload