Adding byte arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Adding byte arrays

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    Adding byte arrays

    Hi all,
    Im having a lot of trouble adding an array of bytes containg a hexidecimal number to another in java. Here is an example of what I want to do (adding 2 byte arrays of hex):
    00 00 00 FF
    00 00 00 FF
    -----------
    00 00 01 FE

    Here is what I have so far:

    Code:
    byte[] ary1 = new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0xFF};
    byte[] ary2 = new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0xFF};
    
    for (int i = 0; i < ary1.length; i++) {			
        ary1[i] += ary2[i];			
    }
    This comes out with 00 00 00 FE (using util methods to make the byte array readable as hex). Which is almost right, obviously im missing the overflow. I have no idea how I could detect this in Java, extensive googling has turned up nothing.
    Or maybe there is another simpler way to go about this?
    please help!

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by skrapes
    Or maybe there is another simpler way to go about this?
    This is an approach to manipulate hex-values:
    Code:
            String sHex = "000000FF";
            int iHex = Integer.parseInt(sHex, 16);
            System.out.println(Integer.toHexString(iHex+iHex).toUpperCase());
    Good luck.

    Regards,
    BK

  3. #3
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    What is the algorithm used when you add numbers manually?
    You start at the right column and move to the left!
    When a sum of the columns exceeds 10 you carry 1 to the next column.
    Your code doesn't do it that way!

    To detect a carry when adding bytes, the sum needs to have an extra bit to hold the carry such as would be the case if the sum went into a short or int.
    After adding the two bytes into an int, test for carry by "and"ing the results with 0xff00. There is a carry if the result is NOT zero.
    Remember how we manually did sums: this carry is then added into the next higher(to the left) column.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You can also manipulate your values by using the Byte class's intValue() method - implementation of the Number class's abstract method - then adding the ints. Then you can the Integer class's byteValue() method to return the byte value. Is this not kosher?
    Last edited by nspils; 09-16-2005 at 11:15 AM.

  5. #5
    Join Date
    Sep 2005
    Posts
    3
    Quote Originally Posted by Norm
    test for carry by "and"ing the results with 0xff00. There is a carry if the result is NOT zero.
    ok this is helpful but its still not quite working, it only seems to be detecting the overflow for some values.
    eg
    000000FF 000000FF = 000001FE (correct)
    000000FF 00000001 = 00000000 (wrong, should be 00000100)
    Seems to not be detecting the smaller overflows. Here is my code now:
    Code:
    byte[] bytes1 = new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01};
    byte[] bytes2 = new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0xFF};
    
    int overflow; byte carry = 0x00;
    for (int i = bytes1.length - 1; i >= 0; i--) {				
    	overflow = (bytes1[i] + bytes2[i] + carry);
            bytes[i] = (byte) overflow;
    	overflow &= 0xFF00;
    	if (overflow != 0) carry = 0x01;
    	else carry = 0x00;			
    }
    Any ideas as to why this only detects the overflow some of the time?

    The other approach by prometheuzz seems to work but would still like to get to the bottom of this overflow method.
    thanks for contributions so far

  6. #6
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    I'd suggest that you put some println() statements in your code to see what is happening. Be sure to display all the values.
    When you see what is happening, you will know how to fix it.

  7. #7
    Join Date
    Sep 2005
    Posts
    3
    yea thanks my actual code does have heaps of printlns, i took them out for the post- the problem occurs with the line:
    overflow &= 0xFF00;
    Its only detecting some of the overflows. not sure how to fix

  8. #8
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Without seeing your input and output, I can only guess what's wrong.
    And that guess is:
    A byte is a very short signed integer. When a byte is assigned to a longer interger variable, the sign is spread. A signed byte holds the values from 127 to -127. Some of your bytes have the high order bit set making them negative. For example 0xFF is -1.
    To make your bytes unsigned, ie keep the high order bit from spreading, you need to assign the byte to an int and then AND it with the mask: 0xFF to strip off the spread sign bits.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2002, 07:46 PM
  2. SOS:: Structure Sizes to Byte Arrays!
    By gautam zalpuri in forum .NET
    Replies: 0
    Last Post: 02-09-2001, 08:17 AM
  3. Byte arrays and File I/O
    By Don in forum .NET
    Replies: 0
    Last Post: 12-15-2000, 10:15 AM
  4. CopyMemory, Variants and Byte Arrays
    By Doug Neary in forum VB Classic
    Replies: 4
    Last Post: 05-24-2000, 06:29 PM
  5. CopyMemory, Variants and Byte Arrays
    By Doug Neary in forum VB Classic
    Replies: 0
    Last Post: 05-23-2000, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center