[공유] 자바 의 Byte 와 기본 유형 간 의 전환

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =http://blog.csdn.net/qiujuer/article/details/45152189 - 학문 의 기원 은 개원 에 사용 된다.초보 자의 마음가짐, 당신 과 함께 격려 하 세 요!
========================================================
오랫동안 블 로 그 를 쓰 지 못 해서 좀 생소 하 다.블 링크 프레임 만 드 느 라 바빴어 요.여기 서 광 고 를 합 니 다. 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 - 학문 의 기원 은 개원 에 사용 된다.초보 자의 마음가짐, 당신 과 함께 격려 하 세 요!
========================================================

좋은 웹페이지 즐겨찾기