Results 1 to 4 of 4
  1. #1
    tiagomac is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Help with bit wise and bit shift operation

    Friends,

    I got two methods to make a conversion from byte array to long and the other to make from long to byte array, the problem is that when i use a number with a negative signal the code broken and the result is not the expected, so, if someone can give me a help i'll be thankful so much cause i tried everything but nothing worked

    byteArrayToLong:
    Java Code:
    /**
    	 * Converts from byte array to long.
    	 * @param b - byte array
    	 * @param size - the length
    	 * @return
    	 */
    	public static long byteArrayToLong(byte[] b, Integer size, boolean autoLength){
    		int length = 0;
    		if (autoLength){
    			length = b.length;
    		}else{
    			length = size;
    		}
    		long ourLong = 0;
    		for (int i = 0; i < length; i++){
    			ourLong |= b[i] & 0xFF;
    			if (i+1 < length)	ourLong <<= 8;
    		}
    		return ourLong;
    	}
    longToByteArray:
    Java Code:
    /**
    	 * Converts long in byte array with the given size.
    	 * @param l - the long value
    	 * @param size - the size (length) to array.
    	 * @return a byte[].
    	 */
    	public static byte[] longToByteArray(long l, int size){
    		byte[] bytes = new byte[size];
    		int p = 0;
    		for (int i = size-1; i >= 0; i--){
    			bytes[i] = (byte) ((l >> p) & 0x0ff);
    			p += 8;
    		}
    		return bytes;
    	}

    A running that gives a good result:
    Java Code:
    	Long ourLong = 65000L;
    	byte[] inBytes = Utils.longToByteArray(ourLong, 2);
    	Long ourLongRecovered = Utils.byteArrayToLong(inBytes, null, true);
    	System.out.println("ourLong: "+ourLong+" ourLongRecovered: "+ourLongRecovered);
    // ourLong: 65000 ourLongRecovered: 65000
    Now with a negative value that give the erro:
    Java Code:
    		Long ourLong = -65000L;
    		byte[] inBytes = Utils.longToByteArray(ourLong, 2);
    		Long ourLongRecovered = Utils.byteArrayToLong(inBytes, null, true);
    		System.out.println("ourLong: "+ourLong+" ourLongRecovered: "+ourLongRecovered);
    // ourLong: -65000 ourLongRecovered: 536
    Last edited by tiagomac; 02-25-2011 at 03:45 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    -650001 doesn't fit in two bytes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tiagomac is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    -650001 doesn't fit in two bytes.

    kind regards,

    Jos
    josAH, sorry, it's not 650001, it's 65000L (to make this a long object), i had to put a L (in caps) in the end to be more visible, sorry...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by tiagomac View Post
    josAH, sorry, it's not 650001, it's 65000L (to make this a long object), i had to put a L (in caps) in the end to be more visible, sorry...
    I should buy me some glasses (or install another font). Your number to byte array method works fine. Vice versa you have to initialize your number to -1 (all bits set to 1) if the most important byte happens to be negative (also has its most important bit set to 1). Otherwise initialize your number to 0.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. where exactly bit shift operators are useful?
    By sandeepsai39 in forum New To Java
    Replies: 6
    Last Post: 09-08-2010, 11:58 AM
  2. I can't make JLabel do what I want (size-wise)
    By fletcher in forum AWT / Swing
    Replies: 9
    Last Post: 10-15-2009, 04:41 AM
  3. Bit Wise operation: shifting
    By rueter in forum New To Java
    Replies: 1
    Last Post: 06-27-2009, 06:51 AM
  4. How rmi preferable than http for Performance wise??
    By Kishore Kumar in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-23-2009, 12:18 PM
  5. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •