Java tutorial
//package com.java2s; /* Copyright (C) 2015 Marius C. Silaghi Author: Marius Silaghi: msilaghi@fit.edu Florida Tech, Human Decision Support Systems Laboratory This program is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation; either the current version of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.math.BigInteger; import java.util.ArrayList; public class Main { public final static BigInteger BN127 = new BigInteger("127"); /** * This is based on bit shifting (using base128()). * Returns array of bytes. * @param val * @return */ public static byte[] toBase_128(BigInteger val) { ArrayList<Integer> al = base128(val); byte[] result = new byte[al.size()]; for (int k = 0; k < result.length; k++) result[k] = al.get(k).byteValue(); ; return result; } /** * Convert to base 128 (bigendian), using shifts. * @param val * @return */ public static ArrayList<Integer> base128(BigInteger val) { ArrayList<Integer> result = new ArrayList<Integer>(); int part = val.and(BN127).intValue(); val = val.shiftRight(7); result.add(0, new Integer(part)); while (!val.equals(BigInteger.ZERO)) { part = val.and(BN127).intValue(); val = val.shiftRight(7); part += 128; result.add(0, new Integer(part)); } ; return result; } }