Java BigDecimal to bigDecimalToBytes(BigDecimal decimal)

Here you can find the source of bigDecimalToBytes(BigDecimal decimal)

Description

This function converts a big decimal to its corresponding byte array format.

License

Open Source License

Parameter

Parameter Description
decimal The big decimal to be converted.

Return

The corresponding byte array.

Declaration

static public byte[] bigDecimalToBytes(BigDecimal decimal) 

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 {
    /**//  w  ww  .j  ava  2s  .  co  m
     * This function converts a big decimal to its corresponding byte array format.
     *
     * @param decimal The big decimal to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] bigDecimalToBytes(BigDecimal decimal) {
        String string = decimal.toString();
        return stringToBytes(string);
    }

    /**
     * This function converts a big decimal into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param decimal The big decimal to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int bigDecimalToBytes(BigDecimal decimal, byte[] buffer) {
        return bigDecimalToBytes(decimal, buffer, 0);
    }

    /**
     * This function converts a big decimal into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param decimal The big decimal to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int bigDecimalToBytes(BigDecimal decimal, byte[] buffer, int index) {
        BigInteger intVal = decimal.unscaledValue();
        int length = 12 + (intVal.bitLength() + 8) / 8;

        int scale = decimal.scale();
        System.arraycopy(intToBytes(scale), 0, buffer, index, 4); // copy in the scale
        index += 4;

        int precision = decimal.precision();
        System.arraycopy(intToBytes(precision), 0, buffer, index, 4); // copy in the scale
        index += 4;

        System.arraycopy(bigIntegerToBytes(intVal), 0, buffer, index, length - 8); // copy in the big integer
        return length;
    }

    /**
     * This function converts a string to its corresponding byte array format.
     *
     * @param string The string to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] stringToBytes(String string) {
        return string.getBytes();
    }

    /**
     * This function converts a string into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param string The string to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int stringToBytes(String string, byte[] buffer) {
        return stringToBytes(string, buffer, 0);
    }

    /**
     * This function converts a string into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param string The string to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int stringToBytes(String string, byte[] buffer, int index) {
        byte[] bytes = string.getBytes();
        int length = bytes.length;
        System.arraycopy(bytes, 0, buffer, index, length);
        return length;
    }

    /**
     * This function converts a integer to its corresponding byte array format.
     *
     * @param i The integer to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] intToBytes(int i) {
        byte[] buffer = new byte[4];
        intToBytes(i, buffer, 0);
        return buffer;
    }

    /**
     * This function converts a integer into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param i The integer to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int intToBytes(int i, byte[] buffer) {
        return intToBytes(i, buffer, 0);
    }

    /**
     * This function converts a integer into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param i The integer to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int intToBytes(int i, byte[] buffer, int index) {
        int length = buffer.length - index;
        if (length > 4)
            length = 4;
        for (int j = 0; j < length; j++) {
            buffer[index + length - j - 1] = (byte) (i >> (j * 8));
        }
        return length;
    }

    /**
     * This function converts a big integer to its corresponding byte array format.
     *
     * @param integer The big integer to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] bigIntegerToBytes(BigInteger integer) {
        return integer.toByteArray();
    }

    /**
     * This function converts a big integer into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param integer The big integer to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int bigIntegerToBytes(BigInteger integer, byte[] buffer) {
        return bigIntegerToBytes(integer, buffer, 0);
    }

    /**
     * This function converts a big integer into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param integer The big integer to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int bigIntegerToBytes(BigInteger integer, byte[] buffer, int index) {
        int length = 4 + (integer.bitLength() + 8) / 8;
        System.arraycopy(intToBytes(length), 0, buffer, index, 4); // copy in the length
        index += 4;
        System.arraycopy(integer.toByteArray(), 0, buffer, index, length - 4); // copy in the big integer
        return length;
    }
}

Related

  1. bigDecimalToByte(BigDecimal num)
  2. bigDecimalToDbString(BigDecimal bd)
  3. bigDecimalToInteger(BigDecimal value)
  4. bigDecimaltoJSONString(Object obj)
  5. bigDecimalToLong(BigDecimal bigDecimal)