Here you can find the source of getByteArrayFromBigDecimalArray(Object value)
Parameter | Description |
---|---|
value | Object to be converted |
public static byte[] getByteArrayFromBigDecimalArray(Object value)
//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 a va 2 s . co m 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; 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 an instance of our value class into a byte[]. * * @param value Object to be converted * * @return converted byte array */ public static byte[] getByteArrayFromBigDecimalArray(Object value) { if (value == null) { return null; } BigDecimal[] a = (BigDecimal[]) value; byte[] total = new byte[a.length * TOTAL_BYTES]; int index = 0; for (int i = 0; i < a.length; i++) { //set signal System.arraycopy(new byte[] { (byte) a[i].signum() }, 0, total, index, NR_SIGNAL_BYTES); index += NR_SIGNAL_BYTES; //set big integer byte[] b = a[i].unscaledValue().abs().toByteArray(); System.arraycopy(b, 0, total, index + (NR_BIGINTEGER_BYTES - b.length), b.length); index += NR_BIGINTEGER_BYTES; //set scale byte[] s = getByteArrayFromIntArray(new int[] { a[i].scale() }); System.arraycopy(s, 0, total, index, NR_SCALE_BYTES); index += NR_SCALE_BYTES; } return total; } /** * Convert an instance of our value class into a byte[]. * * @param value Object to be converted * * @return converted byte array */ public static byte[] getByteArrayFromIntArray(Object value) { if (value == null) { return null; } int[] a = (int[]) value; int n = a.length; byte[] buf = new byte[n * 4]; int i = 0; int j = 0; for (; i < n;) { int x = a[i++]; buf[j++] = (byte) ((x >>> 24) & 0xFF); buf[j++] = (byte) ((x >>> 16) & 0xFF); buf[j++] = (byte) ((x >>> 8) & 0xFF); buf[j++] = (byte) (x & 0xFF); } return buf; } }