📄 hexstring.java
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.util;import java.util.Arrays;/*** HexString is an utility class to perform conversions* between arrays of bytes, various primitive types and their* hexadecimal representations as Strings.* This class cannot be instantiated.** @author Alexander Yap* @version $Revision: 1.3 $ at $Date: 2004/03/12 16:42:11 $ by $Author: alexycyap $*/public abstract class HexString{ private HexString() { // Not used } /** * Returns a byte array from a string of hexadecimal digits. * * @param hex String of hex digits. * @return Array of bytes */ public static byte[] fromString(String hex) { int len = hex.length(); byte[] buf = new byte[((len + 1) / 2)]; int i = 0, j = 0; if ((len % 2) == 1) buf[j++] = (byte) fromDigit(hex.charAt(i++)); while (i < len) { buf[j++] = (byte) ((fromDigit(hex.charAt(i++)) << 4) | fromDigit(hex.charAt(i++))); } return buf; } /** * Returns the number from 0 to 15 corresponding to the hex digit <i>ch</i>. */ public static int fromDigit(char ch) { if (ch >= '0' && ch <= '9') return ch - '0'; if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; throw new IllegalArgumentException("invalid hex digit '" + ch + "'"); } /** * Compares two byte arrays for equality. * * @return true if the arrays have identical contents */ public static boolean areEqual (byte[] a, byte[] b) { return Arrays.equals(a,b); } /** * Returns a string of hexadecimal digits from a byte array. * Each byte is converted to 2 hex digits. */ public static String toString( byte[] ba ) { int length = ba.length; char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length; ) { k = ba[i++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[ k & 0x0F]; } return new String(buf); } private static final char[] HEX_DIGITS = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; /** * Returns a string of hexadecimal digits from a byte. * Each byte is converted to 2 hex digits. */ public static String byteToHex( byte val ) { byte[] ba = new byte[1]; ba[0] = val; return toString( ba ); } /** * Converts hexademical digits to a byte. * Hex string must have at least 4 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static byte hexToByte( String hex) { byte[] ba = fromString(hex); if (ba.length < 1) throw new IllegalArgumentException("Cannot convert "+hex+" to byte."); return ba[0]; } /** * Returns a string of hexadecimal digits from a char. * Each char is converted to 4 hex digits. */ public static String charToHex( char val ) { byte[] ba = new byte[2]; ba[0] = (byte)((val >>> 8) & 0xFF); ba[1] = (byte)(val & 0xFF); return toString( ba ); } /** * Converts hexademical digits to a char. * Hex string must have at least 4 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static char hexToChar( String hex) { byte[] ba = fromString(hex); if (ba.length < 2) throw new IllegalArgumentException("Cannot convert "+hex+" to char."); char val = (char)( ((ba[0]&0xFF)<<8) | (ba[1]&0xFF) ); return val; } /** * Returns a string of hexadecimal digits from a double. * Each double is converted to 16 hex digits. */ public static String doubleToHex( double val ) { return longToHex( Double.doubleToRawLongBits(val) ); } /** * Converts hexademical digits to a double. * Hex string must have at least 16 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static double hexToDouble( String hex) { return Double.longBitsToDouble( hexToLong(hex) ); } /** * Returns a string of hexadecimal digits from a float. * Each float is converted to 8 hex digits. */ public static String floatToHex( float val ) { return intToHex( Float.floatToRawIntBits(val) ); } /** * Converts hexademical digits to a float. * Hex string must have at least 8 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static float hexToFloat( String hex) { return Float.intBitsToFloat( hexToInt(hex) ); } /** * Returns a string of hexadecimal digits from an int. * Each int is converted to 8 hex digits. */ public static String intToHex( int val ) { byte[] ba = new byte[4]; ba[0] = (byte)((val >>> 24) & 0xFF); ba[1] = (byte)((val >>> 16) & 0xFF); ba[2] = (byte)((val >>> 8) & 0xFF); ba[3] = (byte)(val & 0xFF); return toString( ba ); } /** * Converts hexademical digits to an int. * Hex string must have at least 8 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static int hexToInt( String hex) { byte[] ba = fromString(hex); if (ba.length < 4) throw new IllegalArgumentException("Cannot convert "+hex+" to int."); int val = ( ((ba[0]&0xFF)<<24) | ((ba[1]&0xFF)<<16) | ((ba[2]&0xFF)<<8) | (ba[3]&0xFF) ); return val; } /** * Returns a string of hexadecimal digits from a long. * Each long is converted to 16 hex digits. */ public static String longToHex( long val ) { byte[] ba = new byte[8]; ba[0] = (byte)((val >>> 56) & 0xFF); ba[1] = (byte)((val >>> 48) & 0xFF); ba[2] = (byte)((val >>> 40) & 0xFF); ba[3] = (byte)((val >>> 32) & 0xFF); ba[4] = (byte)((val >>> 24) & 0xFF); ba[5] = (byte)((val >>> 16) & 0xFF); ba[6] = (byte)((val >>> 8) & 0xFF); ba[7] = (byte)(val & 0xFF); return toString( ba ); } /** * Converts hexademical digits to a long. * Hex string must have at least 16 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static long hexToLong( String hex) { byte[] ba = fromString(hex); if (ba.length < 8) throw new IllegalArgumentException("Cannot convert "+hex+" to long."); long val = ( ( (((long)ba[0])&0xFF) <<56) | ((((long)ba[1])&0xFF)<<48) | ((((long)ba[2])&0xFF)<<40) | ((((long)ba[3])&0xFF)<<32) | ((((long)ba[4])&0xFF)<<24) | ((((long)ba[5])&0xFF)<<16) | ((((long)ba[6])&0xFF)<<8) | (((long)ba[7])&0xFF) ); return val; } /** * Returns a string of hexadecimal digits from a short. * Each short is converted to 4 hex digits. */ public static String shortToHex( short val ) { byte[] ba = new byte[2]; ba[0] = (byte)((val >>> 8) & 0xFF); ba[1] = (byte)(val & 0xFF); return toString( ba ); } /** * Converts hexademical digits to a short. * Hex string must have at least 4 hex digits. * If there are more digits than required, tailing digits are ignored. */ public static short hexToShort( String hex) { byte[] ba = fromString(hex); if (ba.length < 2) throw new IllegalArgumentException("Cannot convert "+hex+" to short."); short val = (short)( ((ba[0]&0xFF)<<8) | (ba[1]&0xFF) ); return val; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -