0

I need to readin a binary file and then convert it to a byte[] array. I am able to done this with code below, but the problem is the value of the byte array for each element is decimals from signed 2's complement instead of decimals. Is anyone know how to make the byte[] converted to be Decimals instead of Decimal from signed 2's complement?

File inFile = new File(filePath); 
fileByteData = new byte[(int) inFile.length()]; 
DataInputStream dis = new DataInputStream(new FileInputStream(inFile)); 
dis.readFully(fileByteData);
dis.close(); 
user207421
  • 305,947
  • 44
  • 307
  • 483
user9571515
  • 289
  • 5
  • 14
  • Basically you can't represent a `byte` as unsigned. In Java `byte` is signed. What you can do is use a `byte` to store a bit pattern that "means" a number from 0..255, and then convert the `byte` to an unsigned number represented by a larger integer type. If you are consistent in how you use the representations, this can achieve the same effect. – Stephen C Jan 30 '20 at 22:52
  • Bytes are signed in Java, and represented in binary. You haven't stated *why* this is a problem for you. Decimal has nothing to do with it whatsoever. Reading a file already gives you a byte array: no conversion is required. Unclear what you're asking. – user207421 Jan 31 '20 at 01:24
  • @mentallurg Not much point in editing the title if you don't edit the same thing inside the question. You should have left it alone. – user207421 Jan 31 '20 at 01:25

0 Answers0