Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55670
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2018-08-26 01:30
文章分类

全部博文(51)

文章存档

2020年(2)

2018年(49)

我的朋友

分类: Java

2018-08-29 05:40:22

      最近, 在做东西的时候,需要把16进制转换为整数。
      原理和书本一样:
      个位(数) * 16 的0次方、十位(数) * 16 的1次方、百位(数) * 16 的2次方 依此类推。


  1. package my.csdn3;

  2. import java.lang.reflect.Array;
  3. import java.lang.reflect.Constructor;

  4. public class Hex2Int {
  5.     private String name;
  6.     Hex2Int h23;
  7.     public Hex2Int(String name) {
  8.         // TODO Auto-generated constructor stub
  9.         this.name = name;
  10.     }
  11.     
  12.     public Hex2Int() {
  13.         // TODO Auto-generated constructor stub
  14.         
  15.         System.out.println(Hex2Int.class.getConstructors());    
  16.     }
  17.     
  18.     /**
  19.      * @param args
  20.      */
  21.     public static void main(String[] args) {
  22.         // TODO Auto-generated method stub
  23.         Hex2Int h2 = new Hex2Int();
  24.         

  25.         String input;
  26.         if (args.length > 0) {
  27.              input = args[0];
  28.         }
  29.         else {
  30.             input = "0x0a";
  31.         }
  32.         // 84Fa - 34042
  33.         System.out.println(h2.h2i(input));
  34.     }

  35.     public int h2i(String str) {
  36.         String myStr = str.toLowerCase();
  37.         
  38.         int pos = myStr.indexOf("x") + 1;
  39.         int len;
  40.         if(pos == 1) {
  41.             len = myStr.length();
  42.         }
  43.         else {
  44.             len = myStr.length() - pos;
  45.         }
  46.         char[] cA = new char[len];
  47.         for (int i = 0; i < len; i++) {
  48.             cA[i] = myStr.charAt(i + pos);
  49.         }
  50.         
  51.         int result = 0;
  52.         for (int i = 0; i < cA.length; i++) {
  53.             char ch = cA[i];
  54.             int iCh = 0;
  55.             boolean bIllegal = false;
  56.             switch (ch) {
  57.             case '0':
  58.                 iCh = 0;
  59.                 break;
  60.             case '1':
  61.                 iCh = 1;
  62.                 break;
  63.             case '2':
  64.                 iCh = 2;
  65.                 break;
  66.             case '3':
  67.                 iCh = 3;
  68.                 break;
  69.             case '4':
  70.                 iCh = 4;
  71.                 break;
  72.             case '5':
  73.                 iCh = 5;
  74.                 break;
  75.             case '6':
  76.                 iCh = 6;
  77.                 break;
  78.             case '7':
  79.                 iCh = 7;
  80.                 break;
  81.             case '8':
  82.                 iCh = 8;
  83.                 break;
  84.             case '9':
  85.                 iCh = 9;
  86.                 break;
  87.             case 'a':
  88.                 iCh = 10;
  89.                 break;
  90.             case 'b':
  91.                 iCh = 11;
  92.                 break;
  93.             case 'c':
  94.                 iCh = 12;
  95.                 break;
  96.             case 'd':
  97.                 iCh = 13;
  98.                 break;
  99.             case 'e':
  100.                 iCh = 14;
  101.                 break;
  102.             case 'f':
  103.                 iCh = 15;
  104.                 break;

  105.             default:
  106.                 bIllegal = true;
  107.                 break;
  108.             }
  109.             if (!bIllegal) {
  110.                 int iPow = (int) Math.pow(16, cA.length -1 - i);
  111.                 result += iCh * iPow;
  112.             }
  113.             else {
  114.                 result = -1;
  115.             }

  116.         }        
  117.         
  118.         return result;
  119.     }
  120.     
  121. }

阅读(475) | 评论(0) | 转发(0) |
0

上一篇:10进制转换16进制

下一篇:工厂模式

给主人留下些什么吧!~~