1. Byte zu Dezimal
Verwenden Sie die Konvertierung (int) Typ direkt.
/** Byte zu dezimal*/ public static int byte2int (byte b) {int r = (int) b; return r; }2. Dezimaler zu Bytes
Verwenden Sie direkt (BYTE) -Typkonvertierung.
/** Decimal to byte*/ public static byte int2Byte (int i) {byte r = (byte) i; return r; }3. Byte -Array zur hexadezimalen String
Führen Sie für jedes Byte zunächst dieselbe Operation mit 0xff durch und verwenden Sie dann die Funktion Integer. TohexString (). Wenn das Ergebnis nur 1 Bit ist, müssen Sie 0 vorne hinzufügen.
/** Byte -Array zu Hex String*/ public static String bytes2hexString (byte [] b) {String r = ""; für (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. Hexadezimal -String zum Byte -Array
Dies ist komplizierter. Jeder hexadezimale Charakter beträgt 4 Bit und ein Byte 8 Bit, so dass zwei hexadezimale Zeichen in 1 Byte konvertiert werden. Für den ersten Charakter konvertieren Sie es in Byte und verschieben Sie es dann um 4 Bits und betreiben oder arbeiten Sie sie dann mit dem zweiten Charakter -Byte, damit die beiden Zeichen in 1 Byte konvertiert werden.
/** Zeichen werden in Byte konvertiert*/ private statische Byte -Chartobyte (char c) {return (byte) "0123456789abcdef" .Indexof (c); } /** Hex String zum Byte -Array* / public static byte [] 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; Byte [] B = neues Byte [Len]; char [] hc = hex.toarArray (); für (int i = 0; i <len; i ++) {int p = 2*i; B [i] = (Byte) (ChartObyte (HC [p]) << 4 | Chartobyte (HC [P+1])); } return b; }}5. Um ein Byte -Array in eine Zeichenfolge umzuwandeln
Verwenden Sie neue String () direkt.
/** Byte Array to String*/ public static String bytes2string (byte [] b löst Ausnahme aus {String r = new String (b, "utf-8"); return r; }6. String zum Byte -Array
Verwenden Sie GetByTes () direkt.
/** String to Byte Array*/ public static byte [] string2Bytes (string s) {byte [] r = S.GetByTes (); return r; }7. Hexadezimalstring zur String
Konvertieren Sie zuerst in Byte [] und konvertieren Sie dann in die String.
/** HexString to String*/ public static String hex2string (String hex) löst Ausnahme aus {String r = bytes2string (hexstring2Bytes (hex)); return r; }8. String zu hexadezimaler String
Konvertieren Sie zuerst in Byte [] und dann in die hexadezimale Schnur.
/** String zu hexadezimaler String*/ public static String String2hexString (String S) löst Ausnahme aus {String r = bytes2hexString (string2Bytes (s)); return r; }Hauptfunktion:
public static void main (String [] args) löst eine Ausnahme aus {byte b1 = (byte) 45; System.out.println ("1. Byte zu Dezimal:" + byte2int (b1)); int i = 89; System.out.println ("2.1 bis byte zu byte:" + int2Byte (i)); byte [] b2 = new byte [] {(byte) 0xff, (byte) 0x5f, (byte) 0x6, (byte) 0x5a}; System.out.println ("3. Byte -Array zu hexadezimaler String:" + bytes2hexString (B2)); String s1 = new String ("1DA47C"); System.out.println ("4.16% String to Byte Array:" + Arrays.toString (HexString2Bytes (S1))); System.out.println ("5. Byte Array to 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 ("Hallo!"); System.out.println ("8. String to Hexadecimal String:" + String2hexString (S2)); }Auslaufergebnisse:
1. Byte zu Dezimal: 452.1 Dezimales zu Byte: 893. Byte -Array zu Hexadezimalstring: FF5F065A4. Hexadezimale Zeichenfolge zum Byte -Array: [29, -92, 124] 5. Byte -Array zu String:? _ Z6. String to Byte Array: [49, 68, 65, 52, 55, 67] 7. Hexadezimale Zeichenfolge zu String:? | 8. String zu hexadezimaler String: 48656c6f21
In dem obigen Artikel wird kurz die gegenseitige Konvertierung zwischen binären, dezimalen, hexadezimalen und Zeichenfolgen erörtert. Ich hoffe, es kann Ihnen eine Referenz geben und ich hoffe, Sie können Wulin.com mehr unterstützen.