Java BigDecimal Create getBigDecimalArrayFromByteArray(byte[] buf)

Here you can find the source of getBigDecimalArrayFromByteArray(byte[] buf)

Description

Convert a byte[] into an instance of our value class.

License

Open Source License

Parameter

Parameter Description
buf byte array to be converted

Return

converted BigDecimal array as object

Declaration

public static BigDecimal[] getBigDecimalArrayFromByteArray(byte[] buf) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2004 Brendan de Beer and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at//from   ww w.j av  a  2 s.c om
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    
Contributors:
2004 Brendan de Beer - Initial contributor for conversion methods
2005 Erik Bengtson - refactor mapping
2005 Andy Jefferson - added Timestamp/String converters
...
**********************************************************************/

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    private static int NR_BIGINTEGER_BYTES = 40;
    private static int NR_SCALE_BYTES = 4;
    private static int NR_SIGNAL_BYTES = 1;
    private static int TOTAL_BYTES = NR_BIGINTEGER_BYTES + NR_SCALE_BYTES + NR_SIGNAL_BYTES;

    /**
     * Convert a byte[] into an instance of our value class.
     *
     * @param buf byte array to be converted
     *
     * @return converted BigDecimal array as object
     */
    public static BigDecimal[] getBigDecimalArrayFromByteArray(byte[] buf) {
        BigDecimal[] a = new BigDecimal[buf.length / TOTAL_BYTES];

        int index = 0;
        for (int i = 0; i < a.length; i++) {
            //get signal
            byte[] signal = new byte[NR_SIGNAL_BYTES];
            System.arraycopy(buf, index, signal, 0, NR_SIGNAL_BYTES);
            index += NR_SIGNAL_BYTES;

            //get big integer
            byte[] b = new byte[NR_BIGINTEGER_BYTES];
            System.arraycopy(buf, index, b, 0, NR_BIGINTEGER_BYTES);
            BigInteger integer = new BigInteger(signal[0], b);
            index += NR_BIGINTEGER_BYTES;

            //get scale
            byte[] s = new byte[4];
            System.arraycopy(buf, index, s, 0, NR_SCALE_BYTES);
            int[] scale = getIntArrayFromByteArray(s);
            a[i] = new BigDecimal(integer, scale[0]);
            index += NR_SCALE_BYTES;
        }
        return a;
    }

    /**
     * Convert a byte[] into an instance of our value class.
     *
     * @param buf byte array to be converted
     *
     * @return converted int array as object
     */
    public static int[] getIntArrayFromByteArray(byte[] buf) {
        int n = buf.length / 4;
        int[] a = new int[n];
        int i = 0;
        int j = 0;

        for (; i < n;) {
            a[i++] = ((buf[j++] & 0xFF) << 24) + ((buf[j++] & 0xFF) << 16) + ((buf[j++] & 0xFF) << 8)
                    + (buf[j++] & 0xFF);
        }

        return a;
    }
}

Related

  1. getBigDecimal(Object v, BigDecimal defaultValue)
  2. getBigDecimal(Object value)
  3. getBigDecimal(Object value)
  4. getBigDecimal(String aInput)
  5. getBigDecimal(String value)
  6. getBigDecimalByObject(Object obj)
  7. getBigDecimalFrom(double value)
  8. getBigDecimalFromByteArray(byte[] bytes, int start, int length, int scale)
  9. getBigDecimalFromPrimitiveTypes(int input, int scale, int precision)