Here you can find the source of toBytes(BigDecimal number, int byteLength)
public static byte[] toBytes(BigDecimal number, int byteLength)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.Arrays; public class Main { public static byte[] toBytes(Number number, int byteLength) { if (byteLength <= Long.BYTES) { ByteBuffer buffer = ByteBuffer.allocate(byteLength); buffer.put(Arrays.copyOfRange(ByteBuffer.allocate(Long.BYTES).putLong(number.longValue()).array(), Long.BYTES - byteLength, Long.BYTES)); return buffer.array(); } else {// w w w .j av a 2 s . c o m throw new IllegalArgumentException("length is larger than long bytes"); } } public static byte[] toBytes(BigDecimal number, int byteLength) { return toBytes(number.unscaledValue(), byteLength); } public static byte[] toBytes(BigInteger number, int byteLength) { ByteBuffer buffer = ByteBuffer.allocate(byteLength); byte[] bytes = number.toByteArray(); buffer.position(byteLength - bytes.length); buffer.put(bytes); return buffer.array(); } }