Java 기본 바이트 바이트의 간단한 예 [] 다양한 데이터 유형으로 서로 변환
다음은 바이트 [] 유형을 긴, int, double, float, short, cahr, 객체, 문자열 유형으로 변환하는 예입니다.
소켓 개발 중에는 일반적으로 특정 값 (이 값이 다양한 Java 유형 일 수 있음)을 바이트 [] 유형으로 변환해야합니다. 이런 이유로 다음 예를 요약하고 자주 읽을 수 있도록 게시했습니다.
공개 클래스 테스트 케이스 { /*** 짧은 바이트 배열 변환. */ public static byte [] shortobyte (짧은 숫자) {int temp = 숫자; 바이트 [] B = 새로운 바이트 [2]; for (int i = 0; i <b.length; i ++) {b [i] = new Integer (temp & 0xff) .ByteValue (); // 가장 낮은 비트 비트에서 가장 낮은 비트를 저장합니다. } /*** 바이트 배열에서 짧은 것으로 변환. */ public static short bytetothort (byte [] b) {short s = 0; 짧은 s0 = (짧은) (b [0] & 0xff); // 가장 낮은 비트 짧은 s1 = (short) (b [1] & 0xff); S1 << = 8; s = (짧음) (s0 | s1); 반환 s; } /*** int에서 바이트 배열로 변환. */ public static byte [] inttobyte (int number) {int temp = 숫자; 바이트 [] B = 새로운 바이트 [4]; for (int i = 0; i <b.length; i ++) {b [i] = new Integer (temp & 0xff) .ByteValue (); // 가장 낮은 비트 비트에서 가장 낮은 비트를 저장합니다. } /*** 바이트 배열에서 int로 변환. */ public static int bytetoint (byte [] b) {int s = 0; int s0 = b [0] & 0xff; // 가장 낮은 비트 int s1 = b [1] & 0xff; int s2 = b [2] & 0xff; int s3 = b [3] & 0xff; S3 << = 24; s2 << = 16; S1 << = 8; s = s0 | S1 | S2 | S3; 반환 s; } / *** 긴 유형을 바이트 배열로 변환* / public static byte [] longtobyte (긴 숫자) {long temp = 숫자; 바이트 [] B = 새로운 바이트 [8]; for (int i = 0; i <b.length; i ++) {b [i] = new long (temp & 0xff) .ByteValue (); // 가장 낮은 비트 비트에서 가장 낮은 비트를 저장합니다. } /*** 바이트 배열에서 긴 변환. */ public static long bytetolong (byte [] b) {long s = 0; long s0 = b [0] & 0xff; // 가장 낮은 비트 길이 S1 = B [1] 긴 s2 = b [2] & 0xff; 긴 s3 = b [3] & 0xff; 긴 s4 = b [4] & 0xff; // 가장 낮은 비트 길이 s5 = b [5] & 0xff; 긴 s6 = b [6] & 0xff; 긴 s7 = b [7] & 0xff; // s0은 변하지 않은 s1 << = 8; s2 << = 16; S3 << = 24; S4 << = 8 * 4; S5 << = 8 * 5; S6 << = 8 * 6; S7 << = 8 * 7; S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7; 반환 s; } /*** 이중에서 바이트 배열로 변환. */ public static bd long l = double.doubletolongbits (num); for (int i = 0; i <8; i ++) {b [i] = new long (l) .ByteValue (); l = l >> 8; } 반환 b; } /*** 바이트 배열에서 이중으로 변환. */ public static double getDouble (byte [] b) {long m; m = b [0]; m & = 0xff; m | = ((long) b [1] << 8); m & = 0xffffffff; m | = ((long) b [2] << 16); m & = 0xffffffffff; m | = ((long) b [3] << 24); m & = 0xffffffffffl; m | = ((long) b [4] << 32); m & = 0xfffffffffffffl; m | = ((long) b [5] << 40); m & = 0xfffffffffffffl; m | = ((long) b [6] << 48); m & = 0xfffffffffffffffl; m | = ((long) b [7] << 56); Double.longbitstodouble (M)을 반환합니다. } /*** 플로트에서 바이트 배열로 변환. * /public static void floattobyte (float x) {// float.floattointBits (f)로 int} /*** 변환 BYTE 배열에서 float로 변환합니다. */ public static float getfloat (byte [] b) {// 4 바이트 int accum = 0; for (int shiftby = 0; shiftby <4; shiftby ++) {accum | = (b [shiftby] & 0xff) << shiftby * 8; } return float.intbitstofloat (accum); } /*** char에서 바이트 배열로 변환. */ public static byte [] Chartobyte (char c) {byte [] b = new Byte [2]; b [0] = (byte) ((c & 0xff00) >> 8); b [1] = (바이트) (c & 0xff); 반환 b; } /*** 바이트 배열에서 숯으로 변환. */ public static char bytetochar (byte [] b) {char c = (char) (((b [0] & 0xff) << 8) | (b [1] & 0xff)); 반환 c; } /*** 문자열에서 바이트 배열로 변환. */ public static byte [] stringtobyte (String Str)는 unsupportedencodingexception {return str.getBytes ( "gbk"); } /*** 바이트 배열에서 문자열로 변환. */ public static string bytestostring (byte [] str) {문자열 keyword = null; try {keyword = new String (str, "gbk"); } catch (UnsupportedEncodingException e) {e.printstacktrace (); } 리턴 키워드; } / *** 개체에서 바이트 배열로 변환* / @test public void testObject2bytearRay ()는 ioException, classNotFoundException {// object obj = ""; 정수 [] obj = {1, 3, 4}; // // ByTeARRay의 개체 ByTeARRayOutputStream BO = New ByTearRayoutputStream (); ObjectOutputStream OO = 새로운 ObjectOutputStream (BO); oo.writeobject (obj); 바이트 [] bytes = bo.tobytearray (); bo.close (); oo.close (); System.out.println (Arrays.toString (bytes)); integer [] intarr = (integer []) testbytearray2object (bytes); System.out.println (arrays.aslist (intarr)); 바이트 [] b2 = inttobyte (123); System.out.println (Arrays.toString (B2)); int a = bytetoint (b2); System.out.println (a); } /*** 바이트 배열에서 개체로 변환. */ private object testByTearRay2Object (byte [] bytes)는 ioException, classNotFoundException {// byte [] bytes = null; 객체 obj; // BytearRay to Object BytearRayInputStream bi = 새로운 BytearRayInputStream (바이트); ObjectInputStream oi = 새로운 ObjectInputStream (Bi); obj = oi.readobject (); bi.close (); oi.close (); System.out.println (OBJ); 반환 obj; }}읽어 주셔서 감사합니다. 도움이되기를 바랍니다. 이 사이트를 지원 해주셔서 감사합니다!