java 4位byte转为int类型

由于要将一段C语言代码翻译成java

void ReadFile(Point data[][MAX_LENGTH],CString path,int n)
{
	CString str="";
    	Initialize(data);
	FILE * fp;
	CString filename;
	int i,j;
	str.Format("%d",n-1);	
	filename=path;
	filename+="/";
	for (i=0;i<3-str.GetLength();i++)
		filename+="0";
	filename+=str;
	filename+=".dat";
	if ((fp=fopen(filename,"rb"))==NULL)
	{
		//printf("Can not create the file.\n");
		return;
	}
	int t=-1;
	i=0;
	j=0;
	while (!feof(fp))
	{
		fread(&t, sizeof(int), 1, fp);
		if (t != -1)
		{
			data[i][j].x=t;
			fread(&t, sizeof(int), 1, fp);
			data[i][j].y=t;
			j++;
		}
		else
		{
			fread(&t, sizeof(int), 1, fp);
			i++;
			j=0;
		}
		if ((i>=MAX_STROKE)||(j>=MAX_LENGTH))
			break;
	}
	fclose(fp);
}

而最关键的文件操作,一次从文件里面读取四个字节的数据存放到data[i][j].x和data[i][j].y中

由于java只能读取到byte中,写了一个 byte2int的转换h函数

         public void ReadFile(Point[][] data, String path, int n) {
		Initialize(data);
		int i, j;
		String str = String.valueOf(n - 1);
		StringBuilder filename = new StringBuilder();
		filename.append(path).append("/");
		for (i = 0; i < 3 - str.length(); i++)
			filename.append("0");
		filename.append(str).append(".dat");
		File file = new File(filename.toString());
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			i = 0;
			j = 0;
			byte[] t = new byte[4];
			int tint, len;
			while ((len = fis.read(t)) <= 0) {
				tint = byte2int(t, len);
				if (tint != -1) {
					data[i][j].x = tint;
					len = fis.read(t);
					data[i][j].y = byte2int(t, len);
					j++;
				} else {
					fis.read(t);
					i++;
					j = 0;
				}
				if (i >= Constants.MAX_STROKE || j >= Constants.MAX_LENGTH)
					break;
			}
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
最开始的byte2int转换函数的写法

	public static int byte2int(byte[] data,int len) {
		int res = 0;
		switch(len) {
		case 1:{
			res = res |data[0];
			return res;
		}
		case 2:{
			res = res |data[1];
			res = res << 8;
			res = res |data[0];
			return res;
		}
		case 3:{
			res = res |data[2];
			res = res << 8;
			res = res |data[1];
			res = res << 8;
			res = res |data[0];
			return res;
		}
		case 4:{
			res = res | data[3];
			res = res << 8;
			res = res | data[2];
			res = res << 8;
			res = res | data[1];
			res = res << 8;
			res = res | data[0];
			return res;
		}
		default:return res;
		}
	}

然后我发现只有一部分数据正确,而且只要data中存在负数就不正确了,之后在调试之后发现

res = res | data[0] 这一步的时候,实际上是执行的 res = res | (int)data[0] 操作,当data[0]位负数的时候,高位全部补1,导致的结果错误

然后进行改进

        public static int byte2int(byte[] data, int n) {
		switch (n) {
		case 1:
			return (int) data[0];
		case 2:
			return (int) (data[0] & 0xff) | (data[1] << 8 & 0xff00);
		case 3:
			return (int) (data[0] & 0xff) | (data[1] << 8 & 0xff00) | (data[2] << 16 & 0xff0000);
		case 4:
			return (int) (data[0] & 0xff) | (data[1] << 8 & 0xff00) | (data[2] << 16 & 0xff0000)
					| (data[3] << 24 & 0xff000000);
		default:
			return 0;
		}
	}


你可能感兴趣的:(学习笔记)