Java BigDecimal from bytesToBigDecimal(byte[] buffer)

Here you can find the source of bytesToBigDecimal(byte[] buffer)

Description

This function converts the bytes in a byte array to its corresponding big decimal value.

License

Open Source License

Parameter

Parameter Description
buffer The byte array containing the big decimal.

Return

The corresponding big decimal value.

Declaration

static public BigDecimal bytesToBigDecimal(byte[] buffer) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/

import java.math.*;

public class Main {
    /**//ww  w  .j  a  v  a 2 s .c o m
     * This function converts the bytes in a byte array to its corresponding big decimal value.
     *
     * @param buffer The byte array containing the big decimal.
     * @return The corresponding big decimal value.
     */
    static public BigDecimal bytesToBigDecimal(byte[] buffer) {
        String string = bytesToString(buffer);
        return new BigDecimal(string);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding big decimal value.
     *
     * @param buffer The byte array containing the big decimal.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding big decimal value.
     */
    static public BigDecimal bytesToBigDecimal(byte[] buffer, int index) {
        int scale = bytesToInt(buffer, index);
        index += 4;
        int precision = bytesToInt(buffer, index);
        index += 4;
        BigInteger intVal = bytesToBigInteger(buffer, index);
        return new BigDecimal(intVal, scale, new MathContext(precision));
    }

    /**
     * This function converts the bytes in a byte array to its corresponding string value.
     *
     * @param buffer The byte array containing the string.
     * @return The corresponding string value.
     */
    static public String bytesToString(byte[] buffer) {
        return bytesToString(buffer, 0, buffer.length);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding string value.
     *
     * @param buffer The byte array containing the string.
     * @param index The index for the first byte in the byte array.
     * @param length The number of bytes that make up the string.
     * @return The corresponding string value.
     */
    static public String bytesToString(byte[] buffer, int index, int length) {
        return new String(buffer, index, length);
    }

    /**
     * This function converts the bytes in a byte array to its corresponding integer value.
     *
     * @param buffer The byte array containing the integer.
     * @return The corresponding integer value.
     */
    static public int bytesToInt(byte[] buffer) {
        return bytesToInt(buffer, 0);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding integer value.
     *
     * @param buffer The byte array containing the integer.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding integer value.
     */
    static public int bytesToInt(byte[] buffer, int index) {
        int length = buffer.length - index;
        if (length > 4)
            length = 4;
        int integer = 0;
        for (int i = 0; i < length; i++) {
            integer |= ((buffer[index + length - i - 1] & 0xFF) << (i * 8));
        }
        return integer;
    }

    /**
     * This function converts the bytes in a byte array to its corresponding big integer value.
     *
     * @param buffer The byte array containing the big integer.
     * @return The corresponding big integer value.
     */
    static public BigInteger bytesToBigInteger(byte[] buffer) {
        return new BigInteger(buffer);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding big integer value.
     *
     * @param buffer The byte array containing the big integer.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding big integer value.
     */
    static public BigInteger bytesToBigInteger(byte[] buffer, int index) {
        int length = bytesToInt(buffer, index); // pull out the length of the big integer
        index += 4;
        byte[] bytes = new byte[length];
        System.arraycopy(buffer, index, bytes, 0, length); // pull out the bytes for the big integer
        return new BigInteger(bytes);
    }
}

Related

  1. byteToBigDecimal(byte[] raw)
  2. byteToBigDecimal(byte[] raw)
  3. castBigDecimal(Object o)
  4. castToBigDecimal(Object value)