Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came across a similar Problem...</p> <p>The class form the first post to decode/parse the PackedDecimals worked fine... but the code in Gilbert Le Blancs answer did not produce a valid output.</p> <p>so i fixed his code...</p> <pre><code>public class PackedDecimal { private static final int PlusSign = 0x0C; // Plus sign private static final int MinusSign = 0x0D; // Minus private static final int NoSign = 0x0F; // Unsigned private static final int DropHO = 0xFF; // AND mask to drop HO sign bits private static final int GetLO = 0x0F; // Get only LO digit public static long parse(byte[] pdIn) throws Exception { 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; log("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) { System.out.println(); for (int x = 0; x &lt; pdIn.length; x++) { System.out.print(Integer.toString(pdIn[x] &amp; 0x000000ff, 16)); } System.out.println(); throw new Exception("OC7"); } } } else { int digit = aByte &gt;&gt; 4; // HO first val = val * 10 + digit; log("digit=" + digit + ", val=" + val); digit = aByte &amp; GetLO; // now LO val = val * 10 + digit; log("digit=" + digit + ", val=" + val); } } return val; } public static byte[] format(long number, int byteCount) { byte[] bytes = new byte[byteCount]; String data = Long.toString(number); int length = data.length(); boolean isNegative = false; if (data.charAt(0) == '-') { isNegative = true; data = data.substring(1); length--; } if (length % 2 == 0) { data = "0" + data; length++; } int neededBytes = (int) (((length + 1) / 2f) + 0.5f); int extraBytes = neededBytes - byteCount; if (extraBytes &lt; 0) { // Pad extra byte positions with zero for (int i = 0; i &lt; -extraBytes; i++) { bytes[i] = 0x00; } } else if (extraBytes &gt; 0) { // Truncate the high order digits of the number to fit data = data.substring(extraBytes); length -= extraBytes; extraBytes = 0; } // Translate the string digits into bytes for (int pos = 0; pos &lt;= length - 1; pos++) { String digit = data.substring(pos, pos + 1); int now = (pos / 2) - extraBytes; if (pos % 2 == 0) { // High bytes[now] = (byte) (Byte.valueOf(digit) &lt;&lt; 4); log("HIGH " + digit); } else { // Low bytes[now] = (byte) (bytes[now] | (Byte.valueOf(digit) &amp; 0x0f)); log("LOW " + digit); } } // Add the sign byte if (isNegative) { bytes[byteCount - 1] = (byte) (bytes[byteCount - 1] | MinusSign); } else { bytes[byteCount - 1] = (byte) (bytes[byteCount - 1] | PlusSign); } return bytes; } private static void log(String string) { // System.out.println(string); } public static void main(String[] args) throws Exception { long price; byte[] format; price = 44981; format = PackedDecimal.format(price, 5); System.out.println("Input: " + price); System.out.println("Bytes: " + byteToString(format)); System.out.println("Result: " + PackedDecimal.parse(format)); System.out.println("---------"); price = 4498; format = PackedDecimal.format(price, 4); System.out.println("Input: " + price); System.out.println("Bytes: " + byteToString(format)); System.out.println("Result: " + PackedDecimal.parse(format)); System.out.println("---------"); price = 1337; format = PackedDecimal.format(price, 3); System.out.println("Input: " + price); System.out.println("Bytes: " + byteToString(format)); System.out.println("Result: " + PackedDecimal.parse(format)); System.out.println("---------"); } public static String byteToString(byte[] b) { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; b.length; i++) { int curByte = b[i] &amp; 0xFF; sb.append("0x"); if (curByte &lt;= 0x0F) { sb.append("0"); } sb.append(Integer.toString(curByte, 16)); sb.append(" "); } return sb.toString().trim(); } } </code></pre> <p>The results:</p> <pre><code>Input: 44981 Bytes: 0x00 0x00 0x44 0x98 0x1c Result: 44981 --------- Input: 4498 Bytes: 0x00 0x04 0x49 0x8c Result: 4498 --------- Input: 1337 Bytes: 0x01 0x33 0x7c Result: 1337 --------- </code></pre> <p>Have fun &amp; enjoy</p>
    singulars
    1. This table or related slice is empty.
    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.
    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