两个 byte[] 进,一个 byte[] 出。
先看运行结果:
a = 76908BC42C7B53197A9B530052A1B6
b = BF047888D82F2D8802BCE1E5BFFF23403D
ArraysAdd Result = BF04EF1963F35A0355D65C8112FF75E1F3
package CAP;
import java.util.Random;
public class CheckAP {
private static byte[] addBytes(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
private static byte[] replenishArrayToDestinationLength(byte[] A,int replenishLength) {
if(replenishLength < 0 ) return null;
else if(replenishLength == 0) return A;
byte[] replenishLengthRESULT = new byte[A.length + replenishLength];
byte[] ZEROARRAY = new byte[replenishLength];
int i;
for(i=0;i<replenishLength;i++)ZEROARRAY[i] = 0;
replenishLengthRESULT = addBytes(ZEROARRAY,A);
return replenishLengthRESULT;
}
public static byte[] ArraysAdd(byte[] a,byte b[]) {
if(a == null && b == null) {
byte[] cc = new byte[1];
cc[0] = 0;
return cc;
}else if(a == null)return b;
else if(b == null)return a;
int lengtha = a.length,lengthb = b.length;
do {
if(lengtha < lengthb) {
a = replenishArrayToDestinationLength(a,lengthb-lengtha);
lengtha = a.length;
}else if(lengtha > lengthb) {
b = replenishArrayToDestinationLength(b,lengtha-lengthb);
lengthb = b.length;
}
}while(lengtha != lengthb);
byte[] c = new byte[lengtha + 1];
c[c.length - 1] = 0;
byte[] RESULT = new byte[lengtha + 1];
int i;
int inta,intb,intc;
for(i = RESULT.length;i > 0;i--) {
if(i>1) {
inta = a[i-2] & 0xff;
intb = b[i-2] & 0xff;
intc = c[i-1] & 0xff;
if(inta + intb + intc < 256) RESULT[i-1] = (byte) (inta + intb + intc);
else {
c[i-2] = 1;
RESULT[i-1] = ((byte) (inta + intb + intc - 256));
}
}else if(c[0] == 1)RESULT[0] = 1;
}
if(RESULT[0] == 0) {
byte[] NEWRESULT = new byte[RESULT.length-1];
System.arraycopy(RESULT, 1, NEWRESULT, 0, RESULT.length - 1);
return NEWRESULT;
}
return RESULT;
}
public static void main(String[] yzy) {
byte[] a = randomarrays(15);
byte[] b = randomarrays(17);
if(a != null) System.out.println("a = " + bytearrayToHex(a).toUpperCase());
if(b != null) System.out.println("b = " + bytearrayToHex(b).toUpperCase());
System.out.println("ArraysAdd Result = " + bytearrayToHex(ArraysAdd(a,b)).toUpperCase());
}
private static String byteToHex(byte b){
String hex = Integer.toHexString(b & 0xFF);
if(hex.length() < 2){
hex = "0" + hex;
}
return hex;
}
private static String bytearrayToHex(byte[] b) {
String hex= new String("");
int lengthofb;
for(lengthofb=0;lengthofb<b.length;lengthofb++) {
hex=hex + byteToHex(b[lengthofb]);
}
return hex;
}
private static byte[] randomarrays(int lengthofarray) {
byte[] a = new byte[lengthofarray];
int i;
Random r = new Random();
for(i=0;i<lengthofarray;i++) a[i]=(byte) r.nextInt(256);
return a;
}
}
主方法 ArraysAdd 上面的两个方法是必备的,replenishArrayToDestinationLength 和 addBytes。这是主方法 ArraysAdd 所依赖的。
replenishArrayToDestinationLength 是把一个短的数组前面补充指定个 0
addBytes 是把两串字节数组拼接起来
下面的三个方法,可要可不要:
byteToHex 把一个字节变量的值转成十六进制的字符串值,byte 进,String 出
bytearrayToHex 把一串字节数组转成十六进制的字符串值,byte[] 进,String 出
randomarrays 产生指定长度的随机字节,int 进,byte[] 出
主方法:ArraysAdd
两个 byte[] 进,一个 byte[] 出
两个数组当成小学做加法那样个位、十位、百位、千位依次相加。最后返回相加后的结果。
特殊情况:
1.进去的两个 byte[] 中有一个是 null ,则返回另一个 byte[]
2.进去的两个 byte[] 都是null ,则返回一个长度为 1 的数组,该数组的值为:0