栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 16进制转换为10进制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 16进制转换为10进制

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            System.out.println(Integer.valueOf(in.nextLine().substring(2),16));
        }
        
    }
}

Integer.valueOf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import java.util.Scanner;

import java.lang.Math;

public class Main{

    public static void main(String[] args){

        Scanner in = new Scanner(System.in);

        while(in.hasNextLine()){

            String s = in.nextLine();    //读入数字

             

            int count = 0;    //记录转换后的数字

             

            for(int i=0; i < s.length()-2; i++){

                //由于前面两位是'0x',故从第三位开始

                char tc = s.charAt(i+2);    

                int t = 0;    //记录字母转换成的数值

                 

                //将字母转换为数值

                if(tc>='0' && tc<='9')

                    t = tc - '0';

                //字母'A'/'a'~'F''f'对应数字10~15

                else if(tc>='A' && tc<='F')

                    t = tc - 'A' + 10;

                else if(tc>='a' && tc<='f')

                    t = tc - 'a' +10;

                //计算加和

                count += t * Math.pow(16, s.length()-i-3);

            }

            System.out.println(count);

        }

    }

}

Long.parseLong(line.substring(2) , 16)

Math.pow(16, i)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963904.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号