一个用于网络传输,转换数据类型到byte的例子

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class LearnInOutPutBuffer {

	ByteArrayOutputStream os = new ByteArrayOutputStream();

	public ByteArrayOutputStream writeOut(){
		byte[] bmap = new byte[250];
		for(int i=0; i<200; i++) bmap[i] = (byte) i;
		
		String name = "helloq";
		byte[] bname = name.getBytes();
		int i=200;
		for(; i<220 && i<200+bname.length; i++) 
			bmap[i] = bname[i-200];
		for(; i<220 ; i++) 
			bmap[i] = 0;
		
		int score = 250;
		byte[] bscore = intToByteArray1(score);
		for( i=220; i<224; i++)
			bmap[i] = bscore[i-220];
		
		byte state = 2;//running
		bmap[224] = state;
		
		try {
			os.write(bmap);
			os.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return os;		
	}

	public void readIn(ByteArrayInputStream is) {
		byte[] receive = new byte[225];
		int receiveLen = 0;
		
		try {
			receiveLen = is.read(receive);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(" ----------------receiver len = " + receiveLen );
		
		//print len
		byte[] map = new byte[200];
		int i=0;
		for(; i<200; i++){
			map[i] = receive[i];
			System.out.println(" -- " + map[i]);
		}
		

		System.out.println(" ------------------------------------- " );
		
		String name = new String();
		byte[] bname = new byte[20];
		
		for(i=200; i<220 && receive[i]!=0; i++) 
			bname[i-200] = receive[i];
		name = bname.toString();

		System.out.println(" name = " + name );

		System.out.println(" ------------------------------------- " );
		
		byte[] bscore = new byte[4];
		for(i=220; i<224 ; i++) 
			bscore[i-220] = receive[i];
		int score = byteArrayToInt(bscore, 0);
		System.out.println("score = " + score);
		System.out.println(" ------------------------------------- " );
		
		byte state = receive[224];

		System.out.println(" state = " + state );
		System.out.println(" ------------------------------------- " );
		
	}

	public static void main(String args[]) {
		LearnInOutPutBuffer lb = new LearnInOutPutBuffer();
		lb.readIn(new ByteArrayInputStream(lb.writeOut().toByteArray()));
	}

	public static byte[] intToByteArray1(int i) {
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}
/*
	public static byte[] intToByteArray2(int i) throws Exception {
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		DataOutputStream out = new DataOutputStream(buf);
		out.writeInt(i);
		byte[] b = buf.toByteArray();
		out.close();
		buf.close();
		return b;
	}
*/
	
	public static int byteArrayToInt(byte[] b, int offset) {
	       int value= 0;
	       for (int i = 0; i < 4; i++) {
	           int shift= (4 - 1 - i) * 8;
	           value +=(b[i + offset] & 0x000000FF) << shift;
	       }
	       return value;
	 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值