1. Octet à décimal
Utilisez directement la conversion de type (int).
/ * * Byte à décimal * / public static int byte2int (byte b) {int r = (int) b; retour R; }2. décimal en octets
Utilisez directement la conversion de type (octet).
/ * * décimal to byte * / public static octet int2byte (int i) {byte r = (byte) i; retour R; }3. Tableau d'octets en chaîne hexadécimale
Pour chaque octet, effectuez d'abord la même opération avec 0xFF, puis utilisez la fonction entière.tohexstring (). Si le résultat n'est que 1 bit, vous devez ajouter 0 devant.
/ * * Byte Array to Hex String * / public static String bytes2hexString (byte [] b) {String r = ""; for (int i = 0; i <b.length; i ++) {String hex = Integer.tohexString (b [i] & 0xff); if (hex.length () == 1) {hex = '0' + hex; } r + = hex.touppercase (); } return r; }4. Chaîne hexadécimale à l'octet Array
C'est plus compliqué. Chaque caractère hexadécimal est de 4 bits et un octet est de 8 bits, donc deux caractères hexadécimaux sont convertis en 1 octet. Pour le premier caractère, convertissez-le en octet, puis déplacez-le par 4 bits, puis faites ou opérez avec le deuxième octet de caractère, afin que les deux caractères soient convertis en 1 octet.
/ * * Les caractères sont convertis en octet * / octet statique privé ChartoByte (char c) {return (byte) "0123456789abcdef" .indexof (c); } / * * Chaîne hexadéciale vers le tableau d'octet * / octet statique public [] hexString2Bytes (String hex) {if ((hex == null) || (hex.equals (""))) {return null; } else if (hex.length ()% 2! = 0) {return null; } else {hex = hex.touppercase (); int len = hex.length () / 2; octet [] b = nouveau octet [len]; char [] hc = hex.tocharArray (); pour (int i = 0; i <len; i ++) {int p = 2 * i; b [i] = (byte) (chartoByte (hc [p]) << 4 | chartoByte (hc [p + 1])); } return b; }}5. Pour convertir un tableau d'octets en une chaîne
Utilisez la nouvelle chaîne () directement.
/ * * BYTE BYTE TO String * / public static String bytes2String (byte [] b) lève l'exception {String r = new String (b, "utf-8"); retour R; }6. String to octet tableau
Utilisez directement GetBytes ().
/ * * String to Byte Array * / public static byte [] String2Bytes (String s) {byte [] r = s.getBytes (); retour R; }7. chaîne hexadécimale à la chaîne
Convertir en octet [] d'abord, puis convertir en chaîne.
/ * * Hexstring to string * / public static String hex2string (String hex) lève l'exception {String r = bytes2String (hexString2Bytes (hex)); retour R; }8. chaîne à la chaîne hexadécimale
Convertir en octet [] d'abord, puis convertir en chaîne hexadécimale.
/ * * String to hexaDeCImal String * / public static String String2HexString (String S) lève l'exception {String r = bytes2HexString (String2Bytes (s)); retour R; }Fonction principale:
public static void main (String [] args) lève une exception {octet b1 = (byte) 45; System.out.println ("1. Byte à décimal:" + byte2int (b1)); int i = 89; System.out.println ("2.1 à l'octet à l'octet:" + int2byte (i)); octet [] b2 = nouveau octet [] {(byte) 0xff, (byte) 0x5f, (byte) 0x6, (byte) 0x5a}; System.out.println ("3. Tableau d'octet vers une chaîne hexadécimale:" + bytes2Hexstring (b2)); String s1 = new String ("1da47c"); System.out.println ("4,16% de chaîne vers le tableau d'octets:" + arrays.tostring (hexString2Bytes (s1))); System.out.println ("5. Tableau d'octet vers String:" + Bytes2String (B2)); System.out.println ("6. String to Byte Array:" + Arrays.ToString (String2Bytes (S1))); System.out.println ("7,16% String to String:" + Hex2String (S1)); String s2 = new String ("Hello!"); System.out.println ("8. String to hexaDeCImal String:" + String2Hexstring (S2)); }Résultats en cours:
1. Octet à décimal: 452.1 décimal à l'octet: 893. Tableau d'octet en chaîne hexadécimale: FF5F065A4. Chaîne hexadécimale en byte Tableau: [29, -92, 124] 5. Tableau d'octet à la chaîne :? _ Z6. String to octet tableau: [49, 68, 65, 52, 55, 67] 7. Chaîne hexadécimale à la chaîne :? | 8. Chaîne à HexaDecimal String: 48656c6c6f21
L'article ci-dessus discute brièvement de la conversion mutuelle entre les bandes binaires, décimales, hexadécimales et les cordes est tout le contenu que je partage avec vous. J'espère que cela pourra vous donner une référence et j'espère que vous pourrez soutenir Wulin.com plus.