• 09-16-2005, 03:33 AM
skrapes
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.
• 09-16-2005, 05:00 AM
prometheuzz
Quote:

Originally Posted by skrapes

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
• 09-16-2005, 09:37 AM
Norm
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.
• 09-16-2005, 10:13 AM
nspils
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?
• 09-16-2005, 07:21 PM
skrapes
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
• 09-16-2005, 08:38 PM
Norm
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.
• 09-16-2005, 08:52 PM
skrapes
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
• 09-17-2005, 09:41 AM
Norm
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.