基本数据类型与byte之间的相互转换

程序猿之所以这么牛逼,不是因为他们多聪明,而是他们善于总结--所以才会这么牛逼

1,两个byte数组的拼接b_1+b_2=b_3

// 字节数组的合拼
		byte[] b_1 = new byte[10];
		byte[] b_2 = new byte[30];
		byte[] b_3 = new byte[b_1.length + b_2.length];
		//字节数组的拼接
		System.arraycopy(b_1, 0, b_3, 0, b_1.length);
		System.arraycopy(b_2, 0, b_3, b_1.length, b_2.length);
		System.out.println(b_3.length);

2,char转byte和byte转char

public static byte[] getByteOfChar(char paramChar) {
        byte[] arrayOfByte = new byte[2];
        arrayOfByte[0] = (byte) paramChar;
        arrayOfByte[1] = (byte) (paramChar >> '\b');
        return arrayOfByte;
    }

byte转char

public static char getChar(byte[] paramArrayOfByte) {
        return (char) (0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8);
    }

3,double转byte和byte转double

public static byte[] getByteOfDouble(double paramDouble) {
        return getByteOfLong(Double.doubleToLongBits(paramDouble));
    }
public static byte[] getByteOfLong(long paramLong) {
        byte[] arrayOfByte = new byte[8];
        arrayOfByte[0] = (byte) (int) (paramLong & 0xFF);
        arrayOfByte[1] = (byte) (int) (0xFF & paramLong >> 8);
        arrayOfByte[2] = (byte) (int) (0xFF & paramLong >> 16);
        arrayOfByte[3] = (byte) (int) (0xFF & paramLong >> 24);
        arrayOfByte[4] = (byte) (int) (0xFF & paramLong >> 32);
        arrayOfByte[5] = (byte) (int) (0xFF & paramLong >> 40);
        arrayOfByte[6] = (byte) (int) (0xFF & paramLong >> 48);
        arrayOfByte[7] = (byte) (int) (0xFF & paramLong >> 56);
        return arrayOfByte;
    }

byte转double

public static double getDouble(byte[] paramArrayOfByte) {
        long l = getLong(paramArrayOfByte);
        System.out.println(l);
        return Double.longBitsToDouble(l);
    }
    public static long getLong(byte[] paramArrayOfByte) {
        return 0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8 | 0xFF0000
                & paramArrayOfByte[2] << 16 | 0xFF000000 & paramArrayOfByte[3] << 24 | 0x0
                & paramArrayOfByte[4] << 32 | 0x0 & paramArrayOfByte[5] << 40 | 0x0
                & paramArrayOfByte[6] << 48 | 0x0 & paramArrayOfByte[7] << 56;
    }

4,float转byte和byte转float

public static byte[] getByteOfFloat(float paramFloat) {
        return getByteOfInt(Float.floatToRawIntBits(paramFloat));
    }
public static byte[] getByteOfInt(int paramInt) {
        byte[] arrayOfByte = new byte[4];
        arrayOfByte[0] = (byte) (paramInt & 0xFF);
        arrayOfByte[1] = (byte) ((0xFF00 & paramInt) >> 8);
        arrayOfByte[2] = (byte) ((0xFF0000 & paramInt) >> 16);
        arrayOfByte[3] = (byte) ((0xFF000000 & paramInt) >> 24);
        return arrayOfByte;
    }

byte转float

public static float getFloat(byte[] paramArrayOfByte) {
        return Float.intBitsToFloat(getInt(paramArrayOfByte));
    }

    public static int getInt(byte[] paramArrayOfByte) {
        return 0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8 | 0xFF0000
                & paramArrayOfByte[2] << 16 | 0xFF000000 & paramArrayOfByte[3] << 24;
    }

  

5,int转byte和byte转int

public static byte[] getByteOfInt(int paramInt) {
        byte[] arrayOfByte = new byte[4];
        arrayOfByte[0] = (byte) (paramInt & 0xFF);
        arrayOfByte[1] = (byte) ((0xFF00 & paramInt) >> 8);
        arrayOfByte[2] = (byte) ((0xFF0000 & paramInt) >> 16);
        arrayOfByte[3] = (byte) ((0xFF000000 & paramInt) >> 24);
        return arrayOfByte;
    }

byte转int

public static int getInt(byte[] paramArrayOfByte) {
        return 0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8 | 0xFF0000
                & paramArrayOfByte[2] << 16 | 0xFF000000 & paramArrayOfByte[3] << 24;
    }

  

6,long转byte和byte转long

public static byte[] getByteOfLong(long paramLong) {
        byte[] arrayOfByte = new byte[8];
        arrayOfByte[0] = (byte) (int) (paramLong & 0xFF);
        arrayOfByte[1] = (byte) (int) (0xFF & paramLong >> 8);
        arrayOfByte[2] = (byte) (int) (0xFF & paramLong >> 16);
        arrayOfByte[3] = (byte) (int) (0xFF & paramLong >> 24);
        arrayOfByte[4] = (byte) (int) (0xFF & paramLong >> 32);
        arrayOfByte[5] = (byte) (int) (0xFF & paramLong >> 40);
        arrayOfByte[6] = (byte) (int) (0xFF & paramLong >> 48);
        arrayOfByte[7] = (byte) (int) (0xFF & paramLong >> 56);
        return arrayOfByte;
    }

byte转long

public static long getLong(byte[] paramArrayOfByte) {
        return 0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8 | 0xFF0000
                & paramArrayOfByte[2] << 16 | 0xFF000000 & paramArrayOfByte[3] << 24 | 0x0
                & paramArrayOfByte[4] << 32 | 0x0 & paramArrayOfByte[5] << 40 | 0x0
                & paramArrayOfByte[6] << 48 | 0x0 & paramArrayOfByte[7] << 56;
    }

6,short转byte和byte转short

public static byte[] getByteOfShort(short paramShort) {
        byte[] arrayOfByte = new byte[2];
        arrayOfByte[0] = (byte) (paramShort & 0xFF);
        arrayOfByte[1] = (byte) ((0xFF00 & paramShort) >> 8);
        return arrayOfByte;
    }

byte转short

public static short getShort(byte[] paramArrayOfByte) {
        return (short) (0xFF & paramArrayOfByte[0] | 0xFF00 & paramArrayOfByte[1] << 8);
    }

7,String转byte和byte转String

public static byte[] getByteOfString(String paramString1, String paramString2) {
        return paramString1.getBytes(Charset.forName(paramString2));
    }

 指定编码格式

public static byte[] getByteOfStringUTF_8(String paramString) {
        return getByteOfString(paramString, "UTF-8");
    }

byte转Sing

public static String getString(byte[] paramArrayOfByte) {
        return getString(paramArrayOfByte, "UTF-8"); //指定编码
    }

    public static String getString(byte[] paramArrayOfByte, String paramString) {
        return new String(paramArrayOfByte, Charset.forName(paramString));
    }

8,文件转Byte

public byte[] file2Byte() {
		byte[] myfileData = null;
		try {
			InputStream is = mContext.getResources()
					.openRawResource(R.raw.json);//android读取assets下的资源文件
			BufferedInputStream reaStream = new BufferedInputStream(is);
			int length = reaStream.available();
			myfileData = new byte[length];
			reaStream.read(myfileData);

		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
		}
		return myfileData;
	}

9,byte转文件

public void byte2File(byte[] data, String path) {
		if (data.length < 3 || path.equals(""))
			return;
		try {
			FileOutputStream imageOutput = new FileOutputStream(new File(path));
			imageOutput.write(data, 0, data.length);
			imageOutput.close();
			System.out.println("Make Picture success,Please find image in "
					+ path);
		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
		}
	}

相关推荐