zoukankan      html  css  js  c++  java
  • 进制转换

    题目描述

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

     1 import java.util.Scanner;
     2 
     3 
     4 public class Convert {
     5 
     6     /**16进制转为10进制
     7      * @param args
     8      */
     9     public static void main(String[] args) {
    10         Scanner sc = new Scanner(System.in);
    11         while(sc.hasNext()){
    12         String s1 = sc.next();
    13         String s2 = s1.substring(2, s1.length());
    14 //System.out.println(s2);
    15         convert(s2);
    16         }
    17     }
    18     
    19     public static void convert(String s){
    20         double sum=0;
    21         int len = s.length();
    22         double[] arr = new double[len];
    23         for(int i=len-1;i>=0;i--){
    24             int j=len-i-1;
    25             switch (s.charAt(i)) {
    26             case '0':
    27                 arr[j]=0;
    28                 break;
    29             case '1':
    30                 arr[j]=1*Math.pow(16, j);
    31                 break;
    32             case '2':
    33                 arr[j]=2*Math.pow(16, j);
    34                 break;
    35             case '3':
    36                 arr[j]=3*Math.pow(16, j);
    37                 break;
    38             case '4':
    39                 arr[j]=4*Math.pow(16, j);
    40                 break;
    41             case '5':
    42                 arr[j]=5*Math.pow(16, j);
    43                 break;
    44             case '6':
    45                 arr[j]=6*Math.pow(16, j);
    46                 break;
    47             case '7':
    48                 arr[j]=7*Math.pow(16, j);
    49                 break;
    50             case '8':
    51                 arr[j]=8*Math.pow(16, j);
    52                 break;
    53             case '9':
    54                 arr[j]=9*Math.pow(16, j);
    55                 break;
    56             case 'A':
    57                 arr[j]=10*Math.pow(16, j);
    58                 break;
    59             case 'B':
    60                 arr[j]=11*Math.pow(16, j);
    61                 break;
    62             case 'C':
    63                 arr[j]=12*Math.pow(16, j);
    64                 break;
    65             case 'D':
    66                 arr[j]=13*Math.pow(16, j);
    67                 break;
    68             case 'E':
    69                 arr[j]=14*Math.pow(16, j);
    70                 break;
    71             case 'F':
    72                 arr[j]=15*Math.pow(16, j);
    73                 break;
    74             default:
    75                 break;
    76             }
    77             sum = sum+arr[j];
    78         }
    79         System.out.println((int)sum);
    80     }
    81 
    82 }

    代码2:没想到,参考别人,用java不要太简单

     1 import java.util.*;
     2 
     3 public class Main {
     4     public static void main(String[] args) {
     5         Scanner sc=new Scanner(System.in);
     6         while (sc.hasNext()){
     7             String str=sc.next().substring(2);
     8             System.out.println(Integer.parseInt(str,16));
     9         }
    10     }
    11 }  

    代码3:和代码一思想略同,但要简略些

     1 import java.util.*;
     2 
     3 public class Test {
     4     public static void main(String[] args) {
     5         Scanner sc = new Scanner(System.in);
     6         while (sc.hasNext()) {
     7             StringBuffer sb = new StringBuffer();
     8             sb.append(sc.next());
     9             String str = sb.reverse().substring(0, sb.length() - 2);
    10             char ch[] = str.toCharArray();
    11             int sum = 0;
    12             for (int i = 0; i < ch.length; i++) {
    13                 if (ch[i] >= 'A' && ch[i] <= 'F') {
    14                     sum += ((ch[i]) - 55) * Math.pow(16, i);
    15                 } else {
    16                     sum += ((ch[i]) - 48) * Math.pow(16, i);
    17                 }
    18             }
    19             System.out.println(sum);
    20         }
    21     }
    22 }
  • 相关阅读:
    [Z]芯片设计经验
    ADF4350初始化程序(verilog)
    基于Altera FPGA的LVDS配置应用一例
    M4K使用率
    榨干FPGA片上存储资源
    ios通讯录复制出来的电话号码两端有隐藏字符串
    PHP做APP接口时,如何保证接口的安全性
    【PHP】微信开放平台---消息加解密-php7.1 使用openssl代替Mcrypt
    Gram矩阵(pytorch)
    数据库范式
  • 原文地址:https://www.cnblogs.com/crazybuddy/p/5312665.html
Copyright © 2011-2022 走看看