LittleEndian从java中的二进制文件读取的有符号值显示为63而不是157

时间:2016-10-17 09:16:52

标签: java binaryfiles endianness

下面的decodingValue是priting 63而不是157.从二进制文件中读取的500个值只对该值有效,问题仍然存在

import java.nio.ByteBuffer;    
import java.nio.ShortBuffer;

byte[] dataBlockBytes;    
decodedSOR = readFileAsString("binary filepath");

int index = 0;    
dataBlockBytes = decodedSOR.substring(index,index+14).getBytes();

int decodedValue= ByteBuffer.wrap(dataBlockBytes,2,4).order(ByteOrder.LITTLE_ENDIAN).getInt()
/**
 * To read File and return as String
 * @param filePath
 * @return
 * @throws IOException
 */
public String readFileAsString(String filePath) throws IOException {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(
            new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
    }
    reader.close();
    //System.out.println("fileData.toString()===> "+fileData.toString());
    return fileData.toString();    
    }

0 个答案:

没有答案