Java Decimal From toDecimal(int value, byte[] buffer, int offset, int length, int itemLength, boolean packed)

Here you can find the source of toDecimal(int value, byte[] buffer, int offset, int length, int itemLength, boolean packed)

Description

Converts a value to DECIMAL or PACF format, writing it into the buffer at the specified position.

License

Open Source License

Parameter

Parameter Description
value the value.
buffer where to write the value.
offset index of the first byte in the buffer.
length the number of digits to write.
itemLength length of the value in bytes.
packed the positive sign to use: 0xC for DECIMAL, 0xF for PACF.

Declaration

public static void toDecimal(int value, byte[] buffer, int offset, int length, int itemLength, boolean packed) 

Method Source Code

//package com.java2s;
/**/*from  w ww  .j  av a  2  s  .  c  om*/
 *  Copyright (c) 2012, 2014 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * 
 * Contributors: 
 *   Mattia Rocchi - Initial API and implementation 
 */

public class Main {
    /**
     * Converts a value to DECIMAL or PACF format, writing it into the buffer at
     * the specified position.
     * 
     * @param value
     *            the value.
     * @param buffer
     *            where to write the value.
     * @param offset
     *            index of the first byte in the buffer.
     * @param length
     *            the number of digits to write.
     * @param itemLength
     *            length of the value in bytes.
     * @param packed
     *            the positive sign to use: 0xC for DECIMAL, 0xF for PACF.
     */
    public static void toDecimal(int value, byte[] buffer, int offset, int length, int itemLength, boolean packed) {
        int bufferIndex = offset + itemLength - 1;

        // Make sure the value is positive and set the sign.
        if (value >= 0) {
            if (packed)
                buffer[bufferIndex] = 0xF;
            else
                buffer[bufferIndex] = 0xC;
        } else {
            value = -value;
            buffer[bufferIndex] = (byte) 0x0D;
        }

        // Write the digits, starting from the end and working backwards.
        boolean wroteLow = true;
        int digitsToWrite = length;
        while (value > 0 && digitsToWrite > 0) {

            // Set the high nibble of the current byte.
            buffer[bufferIndex] |= (byte) ((value % 10) << 4);
            digitsToWrite--;
            wroteLow = false;
            value = value / 10;

            // We're done with this byte.
            bufferIndex--;

            if (value == 0 || digitsToWrite == 0) {
                // We're done with value.
                break;
            }

            // Set the low nibble of the current byte.
            buffer[bufferIndex] = (byte) (value % 10);
            digitsToWrite--;
            wroteLow = true;
            value = value / 10;
        }

        // Fill with zeros if neccessary.
        if (digitsToWrite > 0) {
            if (wroteLow) {
                // Since we just wrote the low nibble of a byte, the high nibble
                // is already a zero. We can move to the next byte.
                digitsToWrite--;
                bufferIndex--;
            }

            while (digitsToWrite > 0) {
                // Zero out two nibbles at a time.
                buffer[bufferIndex] = 0;
                bufferIndex--;
                digitsToWrite -= 2;
            }
        }
    }
}

Related

  1. toDecimal(final String intValue)
  2. toDecimal(float value)
  3. toDecimal(long val, int places)
  4. toDecimal(String coord)
  5. toDecimal(String number)