进制转换

牛客网上的华为笔试题

描述

写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。

数据范围

保证结果在1 \le n \le 2^{31} - 1

输入描述

输入一个十六进制的数值字符串。

输出描述

输出该数值的十进制字符串。不同组的测试用例用\n隔开。

示例1

输入

0xAA

输出

170

代码

假设一个数n是r进制的,要把它转换成十进制,转换公式为: \sum_{i = 0}^{n}{r^i*d_i}i是从低位开始的下标,d_i是第i位下标的十进制值
例如:一个二进制的数1001
\begin{aligned} 1_31_20_11_0 &= 2^3*1+2^2*1+2^1*0+2^0*1 \\&=8+4+0+1 \\&=13 \end{aligned}

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String num = in.nextLine();
        // System.out.println(Integer.parseInt(num.subString(2), 16));
        // System.out.println(valueOfIn16Radix(num));
        System.out.println(parseInt(num, 16));
    }

    public static int parseInt(String num, int radix) {
        int res = 0;
        for (int i = num.length() - 1; i > 1; i--) {
            res += pow(radix, num.length() - 1 - i) * charToDigit(num.charAt(i));
        }
        return res;
    }

    /**
     * 假设c是数字或者字母 不考虑其他符号 将其转化为10进制数值
     * @param c char
     * @return 十进制数值
     */
    public static int charToDigit(char c) {
        int digit = c - '0';
        if (0 <= digit && digit <= 9) {
            return digit;
        }
        int upperLetter = c - 'a';
        return (0 <= upperLetter && upperLetter < 26 ? upperLetter : c - 'A') + 10;

    }

    /**
     * m^n
     * e.g. pow(2, 0) = 1; pow(3, 2) = 9.
     * @param n m
     * @param m n
     * @return m^n
     */
    public static int pow(int n, int m) {
        int res = 1;
        for (int i = 0; i < m; i++) {
            res *= n;
        }
        return res;
    }
    /**
     *
     * @param s
     * @return 10进制数字
     */
    public static int valueOfIn16Radix(String s) {

        // 十六进制
        int radix = 0x10;

        if (s == null || !s.startsWith("0x")) {
            throw new NumberFormatException();
        }

        boolean negative = false;
        int i = 2, len = s.length();
        int limit = -Integer.MAX_VALUE;

        if (len > 2) {
            char firstChar = s.charAt(2);
            if (firstChar < '0') {
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+') {
                    throw new NumberFormatException();
                }
                if (len == 3) {
                    throw new NumberFormatException();
                }
                i++;
            }
            int multmin = limit / radix;
            int result = 0;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                int digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0 || result < multmin) {
                    throw new NumberFormatException();
                }
                result *= radix;
                if (result < limit + digit) {
                    throw new NumberFormatException();
                }
                result -= digit;
            }
            return negative ? result : -result;
        } else {
            throw new NumberFormatException();
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容