最近在学习java知识,遇到一个比较头疼的问题,C++的写的文件,用java来读取,存在很多问题,最主要的是C++的long(4个字节)和java中的long(8个字节)字节数不一样长,所以在读的过程中,要转换一些,这是解决问题的一点心得,分享出来,希望给大家提供帮助。
int poslocation = tranferdataBuffer.getInt();//从文件的bytebuffer中获取4个字节。一般用getInt实现
当poslocation为负数时,具体实现:
int antiposlocation =(~poslocation);
int nCount2=Math.abs(((antiposlocation&0xff)<<24)|((antiposlocation&0xff00)<<8)|((antiposlocation&0xff0000)>>8) |((antiposlocation&0xff000000)>>24)+1);
poslocation为正数时:
int nCount1=((poslocation&0xff)<<24)|((poslocation&0xff00)<<8)|((poslocation&0xff0000)>>8)|((poslocation&0xff000000)>>24);
此时nCount2和nCount1就是C++中long型数据的值了。
c++中的short转换成java中的short
short uditCircstranfer= tranferdataBuffera.getShort();
short nAuditCircsLen = (short) ((short) ((uditCircstranfer&0xff)<<8)|((uditCircstranfer&0xff00)>>8));