How to read unsigned int in Java
I come from a C/C++ background and I just started using Java. Why Java? Well, I thought it would be nice to be able to use my programs as well on other operating systems and machines without the need to modify and recompile the code.
The first big problem I faced was what to do when I want to work with unsigned variables, more explicitly how to put unsigned values I read from binary files into Java-variables, as Java apparently doesn't support unsigned data types.
The solution I found was to read from disk the informations byte-by-byte, put them into an array of bytes, convert them and put the result into the destination variable, which has to be bigger than the original value (because of the sign). Nasty stuff!
So, here is how I do it:
public static void main(String[] args) {
public static long readBytesLittle2Big(DataInputStream d, int iHowmany) throws IOException {
}
To be honest, I don't know if this is 100% correct (how does the int get assigned to the long when the fuction returns? Is there an implicit conversion?), but it seems to work... .