如何在不使用字符串的情况下删除整数的第n个十六进制数字?

问题描述 投票:1回答:1

考虑值n = 0x12345,如何通过执行0x1235(大字节序)获得结果remove(n, 3)

我认为可以通过执行一些步骤来实现:

  • [partA =从索引0targetIndex - 1中提取部分(应返回0x123);
  • [partB =从targetIndex + 1提取到length(value) - 10x5);]
  • 然后,结果可以用((partA << length(partB) | partB)表示,给出0x1235结果。

但是,每个十六进制数字占据4个空格后,我仍然对如何实现它感到困惑。另外,我不知道检索数字长度的好方法。

这可以很容易地用字符串完成,但是我需要在数千次迭代的上下文中使用它,并且不认为Strings是一个好主意。

所以,在没有字符串的情况下删除该内容的好方法是什么?

javascript java hex bit-manipulation
1个回答
0
投票

执行以下操作:

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
© www.soinside.com 2019 - 2024. All rights reserved.