转化为字节流

本文介绍了一个Java工具类,该类提供了将各种基本数据类型(如long、int、float等)与byte数组互相转换的方法。这些方法对于网络通信、文件读写等场景非常有用。

package eccl.socket.tools;

public class ByteOperator{

public ByteOperator(){

}


/**
* 长整形转化为byte类型
*/
public static void putLong(byte[] buf,int offset,long value) {
buf[offset + 0] = (byte) ((value >> 56) & 0xff);
buf[offset + 1] = (byte) ((value >> 48) & 0xff);
buf[offset + 2] = (byte) ((value >> 40) & 0xff);
buf[offset + 3] = (byte) ((value >> 32) & 0xff);
buf[offset + 4] = (byte) ((value >> 24) & 0xff);
buf[offset + 5] = (byte) ((value >> 16) & 0xff);
buf[offset + 6] = (byte) ((value >> 8) & 0xff);
buf[offset + 7] = (byte) ((value >> 0) & 0xff);
}


/**
* byte 型转化为 Long 类型
*/

public static long getLong(byte[] bytes, int index) {
return ((((long) bytes[index + 0] & 0xff) << 56)
| (((long) bytes[index + 1] & 0xff) << 48)
| (((long) bytes[index + 2] & 0xff) << 40)
| (((long) bytes[index + 3] & 0xff) << 32)
| (((long) bytes[index + 4] & 0xff) << 24)
| (((long) bytes[index + 5] & 0xff) << 16)
| (((long) bytes[index + 6] & 0xff) << 8) | (((long) bytes[index + 7] & 0xff) << 0));
}


/**
* 整形转化为byte类型
*/
public static void putInt(byte[] buf,int offset,int value) {
buf[offset+0] = (byte)((value >> 24) & 0xff);
buf[offset+1] = (byte)((value >> 16) & 0xff);
buf[offset+2] = (byte)((value >> 8) & 0xff);
buf[offset+3] = (byte)((value >> 0) & 0xff);
}

/**
* byte类型转换为int型
*/

public static int getInt(byte[] bytes, int index) {
return (int) ((((bytes[index + 0] & 0xff) << 24) | ((bytes[index + 1] & 0xff) << 16) | ((bytes[index + 2] & 0xff) << 8) | ((bytes[index + 3] & 0xff) << 0)));
}


/**
* 浮点型转化为byte类型
*/

public static void putFloat(byte[] buf,int offset,float value){

try {
putInt(buf,offset,Float.floatToIntBits(value));
}
catch (Exception ex) {
System.out.print("convert the float to Byte is error!\n");
}

}

/**
* byte类型转换为浮点型
*/

public static float getFloat(byte[] bytes,int index){

int num = ((bytes[index]<<24)&0xFF000000)|((bytes[index+1]<<16)&0xFF0000)|((bytes[index+2]<<8)&0xFF00)|(bytes[index+3]&0xFF);
float f = Float.intBitsToFloat(num);
return f;
}


/**
* byte 类型转换为 short型
*/

public static short getShort(byte[] bytes, int index) {
return (short) (((bytes[index] << 8) | bytes[index + 1] & 0xff));
}


/**
* 短整形转化为byte类型
*/
public static void putShort(byte[] buf,int offset,short value) {
buf[offset+0] = (byte)((value >> 8) & 0xff);
buf[offset+1] = (byte)((value >> 0) & 0xff);
}


/**
* 字符串转化为byte类型
*/
public static void putStr(byte[] buf,int offset,int length,String value) {
try{
byte[] tmpByte = value.getBytes();
for(int i=length-1;i>=0;i--){
// buf[offset+i] = tmpByte[i];
buf[offset+i] = tmpByte[i];
}
}catch(Exception e)
{
System.out.print("convert the String to Byte is error!\n"+e);
}
}


/**
* byte型转换为字符串型
*/

public static String getStr(byte[] bytes,int index){

byte[] str = new byte[4];
for(int i=0;i<4;i++){
str[i] = bytes[index+i];
}

return new String(str);

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值