📄 convert.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.utils;/** * Utilities for conversion between miscellaneous datatypes. * * @version 1.00, 28 December 2001 * @author SH */public class Convert{ /** returns hex character for each digit, 0-15 */ private static final char[] lookup_hex = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; /** * Not to be instantiated */ private Convert() {;} // ---------------------------------------------------------------------- // Temperature conversions // ---------------------------------------------------------------------- // ??? does this optimization help out on TINI, where double-division is // ??? potentially slower? If not, feel free to delete it. // ??? /** cache the value of five divided by nine, which is irrational */ private static final double FIVE_NINTHS = (5.0d / 9.0d); /** * Converts a temperature reading from Celsius to Fahrenheit. * * @param celsiusTemperature temperature value in Celsius * * @return the Fahrenheit conversion of the supplied temperature */ public static final double toFahrenheit (double celsiusTemperature) { // (9/5)=1.8 return celsiusTemperature*1.8d + 32.0d; } /** * Converts a temperature reading from Fahrenheit to Celsius. * * @param fahrenheitTemperature temperature value in Fahrenheit * * @return the Celsius conversion of the supplied temperature */ public static final double toCelsius (double fahrenheitTemperature) { return (fahrenheitTemperature - 32.0d)*FIVE_NINTHS; } // ---------------------------------------------------------------------- // Long <-> ByteArray conversions // ---------------------------------------------------------------------- /** * This method constructs a long from a LSByte byte array of specified length. * * @param byteArray byte array to convert to a long (LSByte first) * @param offset byte offset into the array where to start to convert * @param len number of bytes to use to convert to a long * * @returns value constructed from bytes */ public static final long toLong (byte[] byteArray, int offset, int len) { long val = 0; len = Math.min(len, 8); // Concatanate the byte array into one variable. for (int i = (len - 1); i >= 0; i--) { val <<= 8; val |= (byteArray [offset + i] & 0x00FF); } return val; } /** * This method constructs a long from a LSByte byte array of specified length. * Uses 8 bytes starting at the first index. * * @param byteArray byte array to convert to a long (LSByte first) * * @returns value constructed from bytes */ public static final long toLong (byte[] byteArray) { return toLong(byteArray, 0, Math.min(8, byteArray.length)); } /** * This method constructs a LSByte byte array with specified length from a long. * * @param longVal the long value to convert to a byte array. * @param byteArray LSByte first byte array, holds bytes from long * @param offset byte offset into the array * @param len number of bytes to get * * @returns value constructed from bytes */ public static final void toByteArray(long longVal, byte[] byteArray, int offset, int len) { int max = offset + len; // Concatanate the byte array into one variable. for (int i = offset; i <max; i++) { byteArray[i] = (byte)longVal; longVal >>>= 8; } } /** * This method constructs a LSByte byte array with 8 bytes from a long. * * @param longVal the long value to convert to a byte array. * @param byteArray LSByte first byte array, holds bytes from long * */ public static final void toByteArray(long longVal, byte[] byteArray) { toByteArray(longVal, byteArray, 0, 8); } /** * This method constructs a LSByte byte array with 8 bytes from a long. * * @param longVal the long value to convert to a byte array. * * @returns value constructed from bytes */ public static final byte[] toByteArray(long longVal) { byte[] byteArray = new byte[8]; toByteArray(longVal, byteArray, 0, 8); return byteArray; } // ---------------------------------------------------------------------- // Int <-> ByteArray conversions // ---------------------------------------------------------------------- /** * This method constructs an int from a LSByte byte array of specified length. * * @param byteArray byte array to convert to an int (LSByte first) * @param offset byte offset into the array where to start to convert * @param len number of bytes to use to convert to an int * * @returns value constructed from bytes */ public static final int toInt (byte[] byteArray, int offset, int len) { int val = 0; len = Math.min(len, 4); // Concatanate the byte array into one variable. for (int i = (len - 1); i >= 0; i--) { val <<= 8; val |= (byteArray [offset + i] & 0x00FF); } return val; } /** * This method constructs an int from a LSByte byte array of specified length. * Uses 4 bytes starting at the first index. * * @param byteArray byte array to convert to an int (LSByte first) * * @returns value constructed from bytes */ public static final int toInt (byte[] byteArray) { return toInt(byteArray, 0, Math.min(4, byteArray.length)); } /** * This method constructs a LSByte byte array with specified length from an int. * * @param intVal the int value to convert to a byte array. * @param byteArray LSByte first byte array, holds bytes from int * @param offset byte offset into the array * @param len number of bytes to get */ public static final void toByteArray(int intVal, byte[] byteArray, int offset, int len) { int max = offset + len; // Concatanate the byte array into one variable. for (int i = offset; i <max; i++) { byteArray[i] = (byte)intVal; intVal >>>= 8; } } /** * This method constructs a LSByte byte array with 4 bytes from an int. * * @param intVal the int value to convert to a byte array. * @param byteArray LSByte first byte array, holds bytes from long * */ public static final void toByteArray(int intVal, byte[] byteArray) { toByteArray(intVal, byteArray, 0, 4); } /** * This method constructs a LSByte byte array with 4 bytes from an int. * * @param longVal the long value to convert to a byte array. * * @returns value constructed from bytes */ public static final byte[] toByteArray(int intVal) { byte[] byteArray = new byte[4]; toByteArray(intVal, byteArray, 0, 4); return byteArray; } // ---------------------------------------------------------------------- // String <-> ByteArray conversions // ---------------------------------------------------------------------- /** * <P>Converts a hex-encoded string into an array of bytes.</P> * <P>To illustrate the rules for parsing, the following String:<br> * "FF 0 1234 567"<br> * becomes:<br> * byte[]{0xFF,0x00,0x12,0x34,0x56,0x07} * </P> * * @param strData hex-encoded numerical string * @return byte[] the decoded bytes */ public static final byte[] toByteArray(String strData) { byte[] bDataTmp = new byte[strData.length()*2]; int len = toByteArray(strData, bDataTmp, 0, bDataTmp.length); byte[] bData = new byte[len]; System.arraycopy(bDataTmp, 0, bData, 0, len); return bData; } /** * <P>Converts a hex-encoded string into an array of bytes.</P> * <P>To illustrate the rules for parsing, the following String:<br> * "FF 0 1234 567"<br> * becomes:<br> * byte[]{0xFF,0x00,0x12,0x34,0x56,0x07} * </P> * * @param strData hex-encoded numerical string * @param bData byte[] which will hold the decoded bytes * @return The number of bytes converted */ public static final int toByteArray(String strData, byte[] bData) { return toByteArray(strData, bData, 0, bData.length); } /** * <P>Converts a hex-encoded string into an array of bytes.</P> * <P>To illustrate the rules for parsing, the following String:<br> * "FF 0 1234 567"<br> * becomes:<br> * byte[]{0xFF,0x00,0x12,0x34,0x56,0x07} * </P> * * @param strData hex-encoded numerical string * @param bData byte[] which will hold the decoded bytes * @param offset the offset into bData to start placing bytes * @param length the maximum number of bytes to convert * @return The number of bytes converted */ public static final int toByteArray(String strData, byte[] bData, int offset, int length) { int strIndex = 0, strLength = strData.length(); int index = offset; int end = length + offset; while(index<end && strIndex<strLength) { char lower = '0', upper; do { upper = strData.charAt(strIndex++); } while(strIndex<strLength && Character.isWhitespace(upper)); if(strIndex<strLength) { lower = strData.charAt(strIndex++); if(Character.isWhitespace(lower)) { lower = upper; upper = '0'; } } else { if(!Character.isWhitespace(upper)) lower = upper; upper = '0'; } bData[index++] = (byte)((Character.digit(upper, 16) << 4) | Character.digit(lower, 16)); } return (index-offset); } /** * Converts a byte array into a hex-encoded String, using the provided * delimeter. * * @param data The byte[] to convert to a hex-encoded string * @return Hex-encoded String */ public static final String toHexString(byte[] data) { return toHexString(data, 0, data.length, ""); } /** * Converts a byte array into a hex-encoded String, using the provided * delimeter. * * @param data The byte[] to convert to a hex-encoded string * @param offset the offset to start converting bytes * @param length the number of bytes to convert * @return Hex-encoded String */ public static final String toHexString(byte[] data, int offset, int length) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -