Cet article décrit la mise en œuvre Java de la classe d'outils de conversion binaire. Partagez-le pour votre référence, comme suit:
Importer java.nio.Charset.Charset; / ** * hexadecimal (abrégé en hex ou indice 16) est un système de transport en mathématiques qui va de 16 à 1. Il est généralement représenté par les nombres 0 à 9 et les lettres A à F (où: a ~ f est 10 ~ 15). <br> * Par exemple, la décimale numéro 57 est écrite en binaire 111001 et l'hexadécimal 39 est écrit en hexadécimal. <br> * Afin de faire la distinction entre les valeurs hexadécimales et décimales, des langues comme Java et C préfixeront 0x devant le numéro hexadécimal, par exemple, 0x20 est 32 en décimal, au lieu de 20 dans Decimal <br> * * Référence: https://my.oschina.net/xinxingeyaah/blog/28746 * @aUTHOR Looly * */public class HexKit { /** * Lowercase character array used to create the output of hexadecimal characters*/ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; / ** * Un tableau de caractères de capital utilisé pour créer la sortie des caractères hexadécimaux * / private static final char [] digits_upper = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', ',', ', e' e ',', 'f' '' '; // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- * @return hex char [] * / public static char [] encodehex (byte [] data) {return encodehex (data, true); } / ** * Convertir un tableau d'octets en un tableau de caractères hexadécimal * * @param str string * @param charset codage * @return hex char [] * / public static char [] encodehex (String Str, Charset Charset); } / ** * Convertir le tableau d'octet en tableau de caractères hexadécimal * * @param data byte [] * @param tolowercase <code> true </code> transfert au format minuscule, <code> false </code> transfert vers un format de majuscules * @return hex char [] * / public static char [] Encodehex (byower [] data, boolean toloncasse) {return Encodehex (tolonnest ? } / ** * Convertir le tableau d'octet en chaîne hexadécimale * * @param data byte [] * @return hexString * / public static static encodehexstr (byte [] data) {return encodehexstr (data, true); } / ** * Convertir le tableau d'octet en chaîne hexagonale * * @param data byte [] * @param tolowercase <code> true </code> transfert au format minuscule, <code> false </code> transfert au format uppercase * @return hexstring * / public static Encodehexstr (data, datacase? Digits_upper); } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- decodehexstr (hexstr.tocharay (), charset); } / ** * Convertir le tableau de caractères hexadéciques en chaîne * * @param hexdata hex char [] * @param charset codage * @return string * / public static string decodehexstr (char [] hexdata, charset); } / ** * Convertir le tableau des caractères hexadécimaux en tableau d'octets * * @param hexdata hex char [] * @return byte [] * @throws runtimeException Si le tableau des caractères hex est une longueur étrange, une exception d'exécution sera lancée * / public static byte [] décodehex (char [] hexdata) {int len = hexdata. if ((Len & 0x01)! = 0) {Throw New RuntimeException ("Nombre impair de caractères."); } octet [] out = nouveau octet [len >> 1]; // Deux caractères forment la valeur hexagonale. pour (int i = 0, j = 0; j <len; i ++) {int f = todigit (hexdata [j], j) << 4; j ++; f = f | todigit (hexdata [j], j); j ++; out [i] = (byte) (f & 0xff); } return out; } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- todigits) {return new String (encodeHex (data, todigits)); } / ** * Convertir le tableau d'octets en tableau de caractères hexadécimaux * * @param data byte [] * @param todigits char [] pour contrôler la sortie * @return hex char [] * / private static char [] encodehex (byte [] data, char [] todigits) {int l = data.length; char [] out = new char [l << 1]; // Deux caractères forment la valeur hexagonale. for (int i = 0, j = 0; i <l; i ++) {out [j ++] = todigits [(0xf0 & data [i]) >>> 4]; out [j ++] = todigits [0x0f & data [i]]; } return out; } / ** * convertir les caractères hexadécimaux en un entier * * @param ch ch hexadecimal char * @param index la position des caractères hexadécimaux dans le tableau des caractères * @return un entier * @throws runtimeexception lorsque Ch n'est pas un personnage hexadécimal juridique, lance une exception d'exécution * / intatic static intatic todigit (char ch, index) {int digit = privil Caractère.digit (Ch, 16); if (numérique == -1) {lancez new RuntimeException ("caractères hexadécimaux illégaux" + ch + "à index" + index); } RETOUR DIGIT; } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bstring.length ()% 8! = 0) Retour null; StringBuffer tmp = new StringBuffer (); int itmp = 0; for (int i = 0; i <bstring.length (); i + = 4) {itmp = 0; pour (int j = 0; j <4; j ++) {itmp + = Integer.parseInt (bstring.substring (i + j, i + j + 1)) << (4 - j - 1); } tmp.append (Integer.tohexString (itmp)); } return tmp.toString (); } / ** * hexstring * @param hexstring * @return * / public static String hex2binary (String hexstring) {if (hexString == null || hexstring.length ()% 2! = 0) return null; String bstring = "", tmp; for (int i = 0; i <hexString.length (); i ++) {tmp = "0000" + Integer.tobinaryString (Integer.ParseInt (hexstring.substring (i, i + 1), 16)); bstring + = tmp.substring (tmp.length () - 4); } return bstring; } / ** * Convertir le binaire en hex * @param buf * @return * / public static String binary2hex (byte buf []) {StringBuffer sb = new StringBuffer (); for (int i = 0; i <buf.length; i ++) {String hex = Integer.tohexString (buf [i] & 0xff); if (hex.length () == 1) {hex = '0' + hex; } sb.append (hex.touppercase ()); } return sb.toString (); } / ** * Convertir Hexstr en binaire * @param hexstr * @return * / public static octet [] hex2byte (String hexstr) {if (hexstr.length () <1) renvoie null; octet [] résultat = nouveau octet [hexstr.length () / 2]; for (int i = 0; i <hexstr.length () / 2; i ++) {int high = Integer.ParseInt (hexstr.substring (i * 2, i * 2 + 1), 16); int low = Integer.ParseInt (hexstr.substring (i * 2 + 1, i * 2 + 2), 16); résultat [i] = (byte) (élevé * 16 + bas); } Retour Résultat; }}PS: Voici quelques outils de conversion et de calcul en ligne pour ce site Web. Je crois que cela vous sera utile:
Outil de conversion arbitraire en ligne:
http://tools.vevb.com/transcoding/hexconvert
Calculatrice standard en ligne:
http://tools.vevb.com/jisuanqi/jsq
Calculatrice scientifique en ligne:
http://tools.vevb.com/jisuanqi/jsqkeexue
Pour plus d'informations sur les algorithmes Java, les lecteurs qui sont intéressés par ce site peuvent afficher les sujets: "Structure de données Java et tutoriel d'algorithme", "Résumé des conseils de nœud de Dom Operation Java", "Résumé du fichier Java et des conseils d'opération de répertoire" et "Résumé des conseils d'opération Java Cache"
J'espère que cet article sera utile à la programmation Java de tous.