zoukankan      html  css  js  c++  java
  • 13:16进制数值字符串转十进制

    13:

    题目描述

    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

    输入描述:

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

    输出描述:

    输出该数值的十进制字符串。

    输入例子:

    0xA

    输出例子:

    10

    note:有系统自带的Integer.parseInt(num,16)可以直接转换成对应进制数

    package prctice01;
    
    import java.util.Scanner;
    
    /*13:
    题目描述
    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
    输入描述:
    输入一个十六进制的数值字符串。
    输出描述:
    输出该数值的十进制字符串。
    输入例子:
    0xA
    输出例子:
    10*/
    public class Ox2Shi {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            while(scanner.hasNextLine())
            {
                String string  = scanner.nextLine();
                string = string.substring(2);
                System.out.println(Func(string));;
            }
            
        }
    //    public static void main(String[] args) {
    //        Scanner scanner = new Scanner(System.in);
    //        while(scanner.hasNextLine())
    //        {
    //            String string = scanner.nextLine();
    //            string = string.substring(2);
    //            int result = Integer.parseInt(string, 16);
    //            System.out.println(result);
    //        }
    //
    //    }
    
        private static int Func(String string) {
            int count = 0;
            char ch;
            int result = 0;
            int temp = 0;
            while(count < string.length()){
                ch = string.charAt(string.length()-count-1);
                if(ch <='Z' && ch >='A'){
                    temp = ch - 'A' + 10;
                }
                else if(ch <= 'z' && ch >= 'a'){
                    temp = ch - 'a' + 10;
                }
                else if(ch <= '9' && ch >= '0'){
                    temp = ch - '0';
                }
                else break;
                result += temp*Math.pow(16, count);
                count++;
            }
            return result;
        }
    
    }
  • 相关阅读:
    GridView跨列
    html的积累
    什么是json?
    关于string
    Effective C# Item38:定制和支持数据绑定
    Effective C# Item44:为应用程序创建特定的异常类
    Effective C# Item42:利用特性简化反射
    Effective C# Item47:选择安全代码
    Effective C# Item43 : 避免过度使用反射
    Effective C# Item39 : 使用.NET验证
  • 原文地址:https://www.cnblogs.com/newcoder/p/5764364.html
Copyright © 2011-2022 走看看