2014-09-24 24 views
1

说明我仍然在Java 我有代码的新手,但我仍然感到困惑,它是如何工作的简介我的二进制转换器

所以任何人都可以向我解释我的代码是如何工作的二进制转换为十六进制? 我对嵌套的for循环的一部分 的有点混淆,请帮助我理解这里的逻辑

继承人我的代码:

import java.io.*; 

public class arrays { 
    public static void main(String[] args) throws IOException { 
     BufferedReader input = new BufferedReader(new InputStreamReader(
       System.in)); 
     // Binary Storage 
     String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", 
       "B", "C", "D", "E", "F" }; 
     String[] binary = { "0000", "0001", "0010", "0011", "0100", "0101", 
       "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", 
       "1110", "1111" }; 
     // For User!, input a value: 
     System.out.print("Input your Hex Number here : "); 
     String userInput = input.readLine(); 
     String result = ""; 

     for (int i = 0; i < userInput.length(); i++) { 
      /* used for separating the value */ 
      char temp = userInput.charAt(i); 
      String temp2 = "" + temp + ""; 
      for (int j = 0; j < hex.length; j++) { 
       if (temp2.equalsIgnoreCase(hex[j])) { 
        result = result + "\n" + temp + "- " + binary[j]; 
       } 
      } 
     } 

     //Main output 
     System.out.println("THE BINARY OF " + userInput + ":" + result); 


    } 
} 
+0

此代码是否工作? – 2014-09-24 21:04:22

+0

@nikpon是的,但它是糟糕的设计。将数据保存在多个数组中并依靠索引来进行相互转换是一个糟糕的想法。 – azurefrog 2014-09-24 21:06:21

+0

如果评论'用于分隔值'说'检查每个字符',会帮助你吗? – 2014-09-24 21:08:15

回答

0

您的代码的工作,但它是低效的。还有一些简化的空间。

首先,你可以做什么,以使你的程序更有效,是建立的十六进制字符的HashMap<Character, String>二进制字符串:

HashMap<Character, String> map = new HashMap<Character, String>(); 
map.put("0", "0000"); 
map.put("1", "0001"); 
// ... 
map.put("F", "1111"); 

现在你不需要内for循环和比较值将会快得多,因为按键查找地图在技术上是恒定的。

更进一步,而是采用了+符号用于连接字符串,使用StringBuilder建立你的结果和速度了一点东西(read why here)。

也出于可读性目的,您应该使用更重要的变量名称而不是temptemp2

这里你的代码怎么看(full running sample link):

// ... 
    StringBuilder result = new StringBuilder(); 

    for (int i = 0; i < userInput.length(); i++) { 
      /* used for separating the value */ 
      char hexVal = userInput.charAt(i); 
      String binary = map.get(hexVal); 
      result.append(binary); 
    } 

    //Main output 
    System.out.println("THE BINARY OF " + userInput + ":" + result.toString()); 

一般来说,这些类型的转换通过转换为十进制值,然后再改正值工作,所以:

binary -> decimal -> hex 
hex -> decimal -> binary 

Java已经有方法可以帮助你做到这一点。

这是如何从一个String包含一个十六进制数的String包含数的二进制值转换:

String hex = "f";        // 15 in hex 
int decimal = Integer.parseInt(hex, 16);   // this gives 15 in decimal (converts from base 16 to base 10) 
String binary = Integer.toBinaryString(decimal); // this gives "1111" 
System.out.println(binary);      // this prints "1111" 

这是如何从一个String包含二进制数转换成String包含数的十六进制值:现在

String binary = "1111";     // 15 in decimal 
int decimal = Integer.parseInt(binary, 2); // this gives 15 in decimal (converts from base 2 to base 10) 
String hex = Integer.toHexString(decimal); // this gives "f" 
System.out.println(hex);     // this prints "f" 

,为你的程序,你可以使用这些方法,你不需要String[] hexString[] binary个阵列或for循环:现在

public static void main(String[] args) throws IOException { 
    BufferedReader input = new BufferedReader(new InputStreamReader(
      System.in)); 

    // For User!, input a value: 
    System.out.print("Input your Hex Number here : "); 
    String userInput = input.readLine(); 

    // conversion from hex to binary in one line, ain't that awesome :) 
    String result = Integer.toBinaryString(Integer.parseInt(userInput, 16)); 

    //Main output 
    System.out.println("THE BINARY OF " + userInput + ":" + result); 

} 

,当然,你必须照顾无效的输入与任何醒目的Integer.parseIntInteger.toBinaryString抛出异常,使你自己的功能,将检查是否输入是有效的十六进制数。