Beispiele sind wie folgt:
public class DataTYPechangeHelper { / ** * Konvertieren Sie ein Single-Byte-Byte in ein 32-Bit-int * * @param b * byte * @return convert-Ergebnis * / public static int unsignedByToint (byte b) {return (int) b & 0xff; } / ** * Konvertieren Sie ein Single-Byte-Byte in eine hexadezimale Nummer * * @param b * Byte * @return Convert-Ergebnis * / public static String bytetohex (Byte b) {int i = b & 0xff; return Integer.tohexString (i); } / ** * Ein Array von 4 Bytes in einen 32-Bit-int * * @param BUF * Bytes-Buffer * Die Position in @param byte [] wob int Secondbyte = 0; int dritterbyte = 0; Int Fourthbyte = 0; int index = pos; firstByte = (0x000000ff & ((int) buf [index])); SecondByte = (0x000000ff & ((int) buf [index + 1]); ThirdByte = (0x000000ff & ((int) buf [index + 2]); FourthByte = (0x000000ff & ((int) buf [index + 3]); Index = Index + 4; return ((long) (firstByte << 24 | Secondbyte << 16 | drittbyte << 8 | viertemthByte)) & 0xffffffl; } / ** * 16-Bit kurz in Byte Array * * @param s * Short * @return Byte [] Länge 2 * * / public static byte [] ShortTobytearray (Short S) {byte [] targets = new byte [2]; für (int i = 0; i <2; i ++) {int offset = (targets.length - 1 - i) * 8; Ziele [i] = (byte) ((s >>> offset) & 0xff); } Return -Ziele; } / ** * 32-Bit-Ganzzahlen in Byte-Arrays mit Länge 4 * * @param s * int * @return byte [] * * / public static byte [] inttobytearray (int s) {byte [] targets = New Byte [2] konvertieren; für (int i = 0; i <4; i ++) {int offset = (targets.length - 1 - i) * 8; Ziele [i] = (byte) ((s >>> offset) & 0xff); } Return -Ziele; } / ** * lang nach byte [] * * @param s * long * @return byte [] * * / public static byte [] longtobytearray (long s) {byte [] Ziele = new Byte [2]; für (int i = 0; i <8; i ++) {int offset = (targets.length - 1 - i) * 8; Ziele [i] = (byte) ((s >>> offset) & 0xff); } Return -Ziele; } / ** 32-Bit int to byte []* / public static byte [] int2Byte (int res) {byte [] Ziele = new Byte [4]; Ziele [0] = (Byte) (res & 0xff); // niedrigste Zifferziele [1] = (Byte) ((res >> 8) & 0xff); // zweite Target der niedrigen Ziffer [2] = (Byte) ((Res >> 16) & 0xff); // Die zweite hohe Position ziele [3] = (byte) (byte) (byte) (res >>; Ziele zurückgeben; } / ** * Ein Byte-Array von Länge 2 in 16-Bit int * * @param res * byte [] * @return int * * / public static int byte2int (byte [] res) {// res = InversionByte (res); // A -Byte -Daten werden 24 Bits auf 0x? 000000 verschoben und dann 8 Bit auf 0x00? 00000 int Ziele = (res [0] & 0xff) | ((res [1] << 8) & 0xff00); // | Zeigt Umgebungs- oder Rückgabeziele an; }}Das obige ist der vollständige Inhalt der Conversion -Implementierungsmethode von Java Byte Array und Int, Long, Short, Byte, die Ihnen gebracht wurde. Ich hoffe, jeder wird Wulin.com mehr unterstützen ~