[공유] 자바 의 Byte 와 기본 유형 간 의 전환
========================================================
오랫동안 블 로 그 를 쓰 지 못 해서 좀 생소 하 다.블 링크 프레임 만 드 느 라 바빴어 요.여기 서 광 고 를 합 니 다. Blink 는 Socket 프로 토 콜 의 크로스 플랫폼 프레임 워 크 입 니 다. 현재 자바, Android, C \ # 버 전 을 제공 합 니 다.Blink 를 통 해 Socket 전송 에 대한 비동기 패 키 징 을 실현 할 수 있 습 니 다. 데 이 터 를 직접 보 내 거나 받 아들 일 수 있 습 니 다. 현재 byte, String, File 등 형식 을 직접 보 낼 수 있 습 니 다. 프레임 워 크 를 바 꾸 지 않 고 필요 한 유형 을 직접 확장 할 수 있 습 니 다.
Blink 오픈 소스 프레임 링크
Socket 의 전송 에서 모두 Bytes 의 전송 이기 때문에 자바 의 기본 유형 (char, short, int, long) 과 by te [] 의 전환 과 관련 되 어 스스로 정리 하고 간단하게 한 가지 유형 을 썼 다.
/** * Bit Converter */
public class BitConverter {
/** * Convert char to byte[] * * @param x char * @return bytes */
public static byte[] toBytes(char x) {
return toBytes(x, new byte[2], 0);
}
/** * Convert char to byte[] * * @param x char * @param bytes Dest bytes * @param bytePos Dest pos * @return bytes */
public static byte[] toBytes(char x, byte[] bytes, int bytePos) {
bytes[bytePos++] = (byte) (x);
bytes[bytePos] = (byte) (x >> 8);
return bytes;
}
/** * Convert short to byte[] * * @param x Short * @return bytes */
public static byte[] toBytes(short x) {
return toBytes(x, new byte[2], 0);
}
/** * Convert short to byte[] * * @param x Short * @param bytes Dest bytes * @param bytePos Dest pos * @return bytes */
public static byte[] toBytes(short x, byte[] bytes, int bytePos) {
bytes[bytePos++] = (byte) (x);
bytes[bytePos] = (byte) (x >> 8);
return bytes;
}
/** * Convert int to byte[] * * @param x int * @return bytes */
public static byte[] toBytes(int x) {
return toBytes(x, new byte[4], 0);
}
/** * Convert int to byte[] * * @param x int * @param bytes Dest bytes * @param bytePos Dest pos * @return bytes */
public static byte[] toBytes(int x, byte[] bytes, int bytePos) {
bytes[bytePos++] = (byte) (x);
bytes[bytePos++] = (byte) (x >> 8);
bytes[bytePos++] = (byte) (x >> 16);
bytes[bytePos] = (byte) (x >> 24);
return bytes;
}
/** * Convert long to byte[] * * @param x long * @return bytes */
public static byte[] toBytes(long x) {
return toBytes(x, new byte[8], 0);
}
/** * Convert long to byte[] * * @param x long * @param bytes Dest bytes * @param bytePos Dest pos * @return bytes */
public static byte[] toBytes(long x, byte[] bytes, int bytePos) {
bytes[bytePos++] = (byte) (x);
bytes[bytePos++] = (byte) (x >> 8);
bytes[bytePos++] = (byte) (x >> 16);
bytes[bytePos++] = (byte) (x >> 24);
bytes[bytePos++] = (byte) (x >> 32);
bytes[bytePos++] = (byte) (x >> 40);
bytes[bytePos++] = (byte) (x >> 48);
bytes[bytePos] = (byte) (x >> 56);
return bytes;
}
/** * Convert byte[] to char * * @param bytes bytes * @return char */
public static char toChar(byte[] bytes) {
return toChar(bytes, 0);
}
/** * Convert byte[] to char * * @param bytes bytes * @param index byte start index * @return char */
public static char toChar(byte[] bytes, int index) {
return (char) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));
}
/** * Convert byte[] to short * * @param bytes bytes * @return short */
public static short toShort(byte[] bytes) {
return toShort(bytes, 0);
}
/** * Convert byte[] to short * * @param bytes bytes * @param index byte start index * @return short */
public static short toShort(byte[] bytes, int index) {
return (short) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));
}
/** * Convert byte[] to int * * @param bytes bytes * @return int */
public static int toInt(byte[] bytes) {
return toInt(bytes, 0);
}
/** * Convert byte[] to int * * @param bytes bytes * @param index bytes start index * @return int */
public static int toInt(byte[] bytes, int index) {
return (((bytes[index + 3]) << 24) |
((bytes[index + 2] & 0xff) << 16) |
((bytes[index + 1] & 0xff) << 8) |
((bytes[index] & 0xff)));
}
/** * Convert byte[] to long * * @param bytes bytes * @return long */
public static long toLong(byte[] bytes) {
return toLong(bytes, 0);
}
/** * Convert byte[] to long * * @param bytes bytes * @param index bytes start index * @return long */
public static long toLong(byte[] bytes, int index) {
return ((((long) bytes[index + 7]) << 56) |
(((long) bytes[index + 6] & 0xff) << 48) |
(((long) bytes[index + 5] & 0xff) << 40) |
(((long) bytes[index + 4] & 0xff) << 32) |
(((long) bytes[index + 3] & 0xff) << 24) |
(((long) bytes[index + 2] & 0xff) << 16) |
(((long) bytes[index + 1] & 0xff) << 8) |
(((long) bytes[index] & 0xff)));
}
}
주의해 야 할 것 은 기본 유형 에서 byte [] 로 전환 하 든 byte [] 에서 기본 유형 으로 전환 하 든 모두 낮은 위치 가 앞 에 있 고 뒷 자리 에 있 는 형식 을 사용한다.이것 은 C \ # 의 기본 모드 와 일치 합 니 다. 만약 당신 의 요구 가 높 은 위치 에 있다 면 스스로 순 서 를 바 꿔 야 합 니 다.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =http://blog.csdn.net/qiujuer/article/details/45152189 - 학문 의 기원 은 개원 에 사용 된다.초보 자의 마음가짐, 당신 과 함께 격려 하 세 요!
========================================================
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
React 구성 요소에서 소켓 이벤트 리스너가 여러 번 실행됩니다.기본적이지만 종종 간과되는 사이드 프로젝트를 하면서 배운 것이 있습니다. 이 프로젝트에는 단순히 두 가지 주요 부분이 포함되어 있습니다. 프런트 엔드: 반응 및 재료 UI 백엔드: Express, Typescript...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.