Bei der Netzwerkprogrammierung werden lange und int normalerweise auf native Weise verarbeitet, anstatt für Bandbreitensparen oder -codierung in String zu konvertieren.
public class byteorderutils {public static byte [] int2Byte (int res) {byte [] Ziele = new Byte [4]; Ziele [3] = (Byte) (res & 0xff); // Niedrigste Bitziele [2] = (Byte) ((Res >> 8) & 0xff); // sekundäre Low -Bit -Ziele [1] = (Byte) ((res >> 16) & 0xff); // Sekundäre Hochbitziele [0] = (Byte) (res >>; Ziele zurückgeben; } public static int bytearraytoint (byte [] b) {byte [] a = new Byte [4]; int i = A.Length - 1, j = B.Length - 1; für (; i> = 0; i-, j--) {// Kopieren Sie Daten aus dem Schwanz von B (d. H. Das niedrige Bit des int-Werts) if (j> = 0) a [i] = b [j]; sonst a [i] = 0; // Wenn B.Length weniger als 4 ist, fügen Sie 0 zu dem hohen Bit hinzu} int v0 = (a [0] & 0xff) << 24; // & 0xff konvertiert den Byte -Wert, um die Zeichenbits des hohen Bits nach dem automatischen Java -Automatic -Werbung zu vermeiden, int v1 = (a [1] & 0xff) << 16; int v2 = (a [2] & 0xff) << 8; int v3 = (a [3] & 0xff); return v0 + v1 + v2 + v3; } public static byte [] long2Byte (long Res) {byte [] buffer = new Byte [8]; für (int i = 0; i <8; i ++) {int offset = 64 - (i+1) * 8; Buffer [i] = (Byte) ((res >> offset) & 0xff); } return buffer;} public static long bytearraytolong (byte [] b) {long vales = 0; für (int i = 0; i <8; i ++) {values << = 8; Werte | = (b [i] & 0xff); } Rückgabewerte; }}Das obige ist der vollständige Inhalt der Java -Int -Konvertierung in Byte und Long Conversion in Byte, die Sie vom Herausgeber zu Ihnen gebracht haben. Ich hoffe, jeder wird Wulin.com mehr unterstützen ~