java将两个字符串转为十六进制进行异或的结果是多少进制的,怎么做?
发布网友
发布时间:2022-04-25 22:47
我来回答
共2个回答
热心网友
时间:2022-06-18 08:54
小猪,已解答,采纳即可
public class Rectangle
{
public static void main ( String[] args )
{
String S1 = "0412134FFFFFFFFFF";
String T1 = "00007770000200";
long si = Long.parseLong (S1, 16);
long ti = Long.parseLong (T1);
long st = si ^ ti;
// 很显然是 十进制 0 ~ 9
// 4693090553043039415
System.out.println (st);
String hex = Long.toHexString (st);
// 412134fe30df34b7
System.out.println (hex);
}
}
热心网友
时间:2022-06-18 08:55
public String xorHex(String a, String b) {
char[] chars = new char[a.length()];
for (int i = 0; i < chars.length; i++) {
chars[i] = toHex(fromHex(a.charAt(i)) ^ fromHex(b.charAt(i)));
}
return new String(chars);
}
private static int fromHex(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
throw new IllegalArgumentException();
}
private char toHex(int nybble) {
if (nybble < 0 || nybble > 15) {
throw new IllegalArgumentException();
}
return "0123456789ABCDEF".charAt(nybble);
}
也可以这么写:
String s1 = "426F62";
String s2 = "457665";
int n1 = Integer.parseInt(s1, 16);
int n2 = Integer.parseInt(s2, 16);
int n3 = n1 ^ n2;
String s3 = String.format("%06x", n3);追答亲,两种算法
记得优先采纳哦