Java examples for java.lang:byte Array Convert
Convert byte array to double
/**//w w w. j a va 2s . c om * Syncnapsis Framework - Copyright (c) 2012-2014 ultimate * * This program is free software; you can redistribute it and/or modify it under the terms of * the GNU General Public License as published by the Free Software Foundation; either version * 3 of the License, or 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 MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Plublic License along with this program; * if not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(toDouble(data)); } /** * Convert byte array to double * * @param data - the byte array * @return the double value */ public static double toDouble(byte[] data) { if (data == null || data.length != 8) return 0x0; return Double.longBitsToDouble(toLong(data)); } /** * Convert byte array to long * * @param data - the byte array * @return the long value */ public static long toLong(byte[] data) { if (data == null || data.length != 8) return 0x0; // (Below) convert to longs before shift because digits are lost with ints beyond the 32-bit // limit // @formatter:off return (long) ((long) (0xff & data[0]) << 56 | (long) (0xff & data[1]) << 48 | (long) (0xff & data[2]) << 40 | (long) (0xff & data[3]) << 32 | (long) (0xff & data[4]) << 24 | (long) (0xff & data[5]) << 16 | (long) (0xff & data[6]) << 8 | (long) (0xff & data[7]) << 0); // @formatter:on } }