考虑值n = 0x12345
,如何通过执行0x1235
(大字节序)获得结果remove(n, 3)
?
我认为可以通过执行一些步骤来实现:
partA
=从索引0
到targetIndex - 1
中提取部分(应返回0x123
);partB
=从targetIndex + 1
提取到length(value) - 1
(0x5
);]((partA << length(partB) | partB)
表示,给出0x1235
结果。但是,每个十六进制数字占据4个空格后,我仍然对如何实现它感到困惑。另外,我不知道检索数字长度的好方法。
这可以很容易地用字符串完成,但是我需要在数千次迭代的上下文中使用它,并且不认为Strings是一个好主意。
所以,在没有字符串的情况下删除该内容的好方法是什么?
执行以下操作:
public class Main {
public static void main(String[] args) {
int n = 0x12345;
int temp = n;
int length = 0;
//Find length
while (temp != 0) {
length++;
temp /= 16;
}
System.out.println("Length: " + length);
// Remove digit at index 3
int m = n;
int index = 3;
for (int i = index + 2; i <= length; i++) {
m /= 16;
}
m -= m % 16;
m += n % ((int) (Math.pow(16, length - index - 1)));
System.out.println(Integer.toHexString(m));
}
}
输出:
Length: 5
1235