简体   繁体   English

十六进制到十进制转换问题

[英]Hex to Decimal Conversion Issue

first post here. 首先发布在这里。 I have an assignment to convert from hex to decimal, including other conversions but this one has me stumped... 我有一项任务要从十六进制转换为十进制,包括其他转换,但是这让我很困惑...

So the hex values I need to convert are 12345678, 2A3DF8A7, 00FF00FF. 因此,我需要转换的十六进制值为12345678、2A3DF8A7、00FF00FF。 00FF00FF works fine, it's the number values that are giving me trouble. 00FF00FF正常工作,这是数字值给我带来麻烦。

I know you're supposed to multiply the digit by 16^n, but for some reason I'm getting values that are way too high even though I think I am doing it right. 我知道您应该将数字乘以16 ^ n,但是由于某种原因,我得到的值太高了,尽管我认为我做对了。 Obviously not since it's not working. 显然不是,因为它不起作用。 Help would be greatly appreciated! 帮助将不胜感激!

My code has changed throughout the last hour messing with it, but it's the last "else" now that is the problem. 在过去的一个小时中,我的代码已经改变了,但现在是问题的最后一个“其他”。

public void toDec()
{
    dec = 0;
    for (int j = 0, i = 7; j < hex.length(); j++){

        if (hex.charAt(j) == 'A') {
            dec += (10 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == 'B') {
            dec += (11 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == 'C') {
            dec += (12 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == 'D') {
            dec += (13 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == 'E') {
            dec += (14 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == 'F') {
            dec += (15 * (int)Math.pow(16, i));
        }
        else if (hex.charAt(j) == '0') {
            dec = dec;
        }
        else {
            dec += ((int)hex.charAt(j)) * ((int)Math.pow(16, i));
        }
        i--;
    }
}

Your problem is in your else clause, handling the digits 1 to 9. 您的问题出在else子句中,处理数字1到9。

The problem is that you're confusing the digits with the characters representing them. 问题是您将数字与代表数字的字符混淆了。 For example, the digit 1 is represented by the character '1' , which has a character code of 49. So, when you cast a '1' character to int in your else clause (ie (int)hex.charAt(j) ), what you get back is it's character code of 49, not 1. 例如,数字1由字符'1' ,其字符代码为49。因此,当您在else子句中将'1'字符转换为int时(即(int)hex.charAt(j) ),您得到的是49的字符代码,而不是1。

Now, the easy (and idiomatic) way to fix this is to use integer math on your characters. 现在,解决此问题的简单(惯用方法)是在字符上使用整数数学。 For example, use hex.charAt(j) - '0' instead of (int)hex.charAt(j) . 例如,使用hex.charAt(j) - '0'代替(int)hex.charAt(j) This works because the characters '0' through '9' are assigned consecutive character codes 48 through 57, so (for example) subtracting the character code for '0' (which is 48) from the character code for '1' (which is 49) returns the actual digit value, since 49 - 48 = 1 . 之所以可行,是因为从字符'0''9'被分配了连续的字符代码48到57,因此(例如)从'1'的字符代码(即48)中减去'0'的字符代码(即48)。 49)返回实际数字值,因为49 - 48 = 1 The same works for the other digits, too: '5' - '0' is equivalent to 53 - 48 , which is (of course) equal to 5. 其他数字也相同: '5' - '0'等于53 - 48 ,(当然)等于5。

Even better, the same approach also works for your other hexadecimal characters too! 更好的是,同样的方法也适用于您的其他十六进制字符! For example, if you do 'C' - 'A' + 10 , you'll get the result 12 (which is the hexadecimal value of the letter C). 例如,如果您执行'C' - 'A' + 10 ,您将得到结果12 (这是字母C的十六进制值)。

Casting char to an int is the problem here - instead of int value, representing specific digit, you get char code. 这里的问题是将char强制转换为int -您将获得char代码,而不是用int值表示特定的数字。 Just give it a try: System.out.println((int) '5'); 只需尝试一下: System.out.println((int) '5');

The easiest way to solve that would be (hex.charAt(j) - '0') . 解决该问题的最简单方法是(hex.charAt(j) - '0')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM