java中32位十六进制转换为十进制

发布于 2024-12-06 14:32:23 字数 2068 浏览 5 评论 0原文

对于作业,我必须编写一个程序,该程序将接受 8 个字符串(十六进制),然后将其转换为基数 10。我不允许使用任何外部类来执行此操作。我很确定我可以正常工作......仅适用于正数。我的问题是如何显示负数。 一个例子是 FFFFFFFA 应打印为 -6 到目前为止,这是我的代码,

package hexconverter;

import java.util.*;

/**
 *
 * @author Steven
 */
public class Main {

    Scanner scanner = new Scanner(System.in);

    public void doWork() {



        System.err.println("Please enter the internal representation: ");
        String hex;
        hex = scanner.next();
        hex = hex.toUpperCase();

        long count = 1;
        long ans = 0;

        for (int i = 7; i >= 0; i--) {
            Character c = hex.charAt(i);

            if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9') {
                int num = fixLetters(c);
                ans = ans + (num * count);
                count = count * 16;
            } else {

                String s = c.toString(c);
                long num = Integer.parseInt(s);
                ans = ans + (num * count);
                count = count * 16;
            }
        }

       if (ans > 2147483647) {
            System.out.println("is negative");


       } else {
            System.out.println(ans);
       }
    }

    public int fixLetters(Character c) {
        if (c.equals('A')) {
            return 10;
        } else if (c.equals('B')) {
            return 11;
        } else if (c.equals('C')) {
            return 12;
        } else if (c.equals('D')) {
            return 13;
        } else if (c.equals('E')) {
            return 14;
        } else if (c.equals('F')) {
            return 15;
        } else {
            return 0;
        }

    }

    public static void main(String[] args) {
        // TODO code application logic here
        Main a = new Main();
        a.doWork();
    }
}

我认为我对负整数的测试是正确的...因为这是 32 位可以容纳的最高值,超过该值的任何内容都将是溢出,因此意味着它应该是负数。 从这里我不知道该怎么做。任何指示或提示将不胜感激。如果没有办法从数学上做到这一点,我觉得我必须将十六进制转换为二进制,然后对其执行二进制补码,但我又不知道从哪里开始。

提前致谢

for an assignment I have to write a program that will take in an 8 character string(hexadecimal) and then convert it to base 10. I am not allowed to use any outside classes to do this. I'm pretty sure I have it working properly... for positive numbers only. My problem is how to show negative numbers.
an example is that FFFFFFFA should print out as -6
This is my code so far

package hexconverter;

import java.util.*;

/**
 *
 * @author Steven
 */
public class Main {

    Scanner scanner = new Scanner(System.in);

    public void doWork() {



        System.err.println("Please enter the internal representation: ");
        String hex;
        hex = scanner.next();
        hex = hex.toUpperCase();

        long count = 1;
        long ans = 0;

        for (int i = 7; i >= 0; i--) {
            Character c = hex.charAt(i);

            if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9') {
                int num = fixLetters(c);
                ans = ans + (num * count);
                count = count * 16;
            } else {

                String s = c.toString(c);
                long num = Integer.parseInt(s);
                ans = ans + (num * count);
                count = count * 16;
            }
        }

       if (ans > 2147483647) {
            System.out.println("is negative");


       } else {
            System.out.println(ans);
       }
    }

    public int fixLetters(Character c) {
        if (c.equals('A')) {
            return 10;
        } else if (c.equals('B')) {
            return 11;
        } else if (c.equals('C')) {
            return 12;
        } else if (c.equals('D')) {
            return 13;
        } else if (c.equals('E')) {
            return 14;
        } else if (c.equals('F')) {
            return 15;
        } else {
            return 0;
        }

    }

    public static void main(String[] args) {
        // TODO code application logic here
        Main a = new Main();
        a.doWork();
    }
}

I think my test for a negative integer is correct... since that is the highest value 32 bits can hold, anything over that will be an overflow so that means it should be negative.
From here I have no idea how to go about this. Any pointers or tips would be greatly appreciated. If there is no way to do it mathematically I feel like I am going to have to convert the hex into binary and then perform twos complements on it, but again I dont know where to start.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-12-13 14:32:23

如果代码中的数字为负数 (> 2147483647),只需从中减去 2^32 (4294967296)。然后打印出来。

if (ans > 2147483647) {
        System.out.println(ans - 4294967296L);
   } else {
        System.out.println(ans);
   }

If the number is negative (> 2147483647) in your code, just subtract 2^32 (4294967296) from it. Then print it out.

if (ans > 2147483647) {
        System.out.println(ans - 4294967296L);
   } else {
        System.out.println(ans);
   }
那小子欠揍 2024-12-13 14:32:23

在 32 位 2 的补码二进制表示中,负数的值恰好比无符号表示中相同位模式的值小 2 ^ 32。您已经确定该数字可能为负数;剩下要做的就是减去 2 ^ 32。

当然,2 ^ 32(十进制为 4294967296,或十六进制为 0x100000000)是一个无法用 Java 的“int”类型表示的值,因此您需要使用沿着”:

if (ans > 2147483647) {
    // System.out.println("is negative");
    ans = ans - 0x100000000L;
    System.out.println(ans);
} else {

In 32-bit 2's complement binary representation, the value of a negative is exactly 2 ^ 32 less than the value of the same bit pattern in unsigned representation. You have already identified that the number might be negative; all that's left to do is subtract 2 ^ 32.

Of course, 2 ^ 32 (4294967296 in decimal, or 0x100000000 in hex) is a value that can't be represented by Java's "int" type, so you'll need to use a "long":

if (ans > 2147483647) {
    // System.out.println("is negative");
    ans = ans - 0x100000000L;
    System.out.println(ans);
} else {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文