Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert byte array to int

843785Oct 30 2008 — edited Oct 31 2008
This code is supposedly convert byte array into int. But when I compile I get an error: non-static variable output cannot be referenced from a static context
public class BitArrayToInt{
	byte[] output = {
		(byte)0x00, (byte)0xFF
		};
	int value;
	
	public static void main(String args[]){
		
		System.out.println(byteArrayToInt(output));
		
	}
	
	public static int byteArrayToInt(byte[] b){
		return byteArrayToInt(b, 0);
	}

	private static int byteArrayToInt(byte[] b, int offset){
		
		int value = 0;
		for(int i=0; i<4; i++){
			int shift = (4 - 1 - i)*8;
			value += (b[i + offset] & 0x000000FF) << shift;
		}
		return value;
	}	
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 28 2008
Added on Oct 30 2008
1 comment
330 views