2011-12-27 222 views
11

我想將十六進制字符串轉換爲二進制字符串。例如,Hex 2是0010.下面是代碼:將十六進制字符串轉換爲二進制字符串

String HexToBinary(String Hex) 
{ 
    int i = Integer.parseInt(Hex); 
    String Bin = Integer.toBinaryString(i); 
    return Bin; 
} 

但是,這隻適用於十六進制0 - 9;它不適用於Hex A-F,因爲它使用int。任何人都可以提升它嗎

+0

只是爲了讓你(及其他用戶)知道,Java的約定規定,你應該從變量名的小寫字母開始。例如十六進制,應該是十六進制,並且Bin應該是bin。 – planty182 2014-08-18 07:48:16

回答

25

你需要告訴Java中的int是十六進制,像這樣:

String hexToBinary(String hex) { 
    int i = Integer.parseInt(hex, 16); 
    String bin = Integer.toBinaryString(i); 
    return bin; 
} 
+2

謝謝.................. – user871695 2011-12-27 03:20:57

+0

不會wokr爲一個巨大的十六進制字符串值 – 2017-01-23 11:45:10

+0

@KaveeshKanwal這應該毫不奇怪,因爲OP使用整數,對吧? – dasblinkenlight 2017-01-23 12:00:39

9

接受的版本將僅適用於32位數字的工作。

下面是對任意長的十六進制字符串工作的版本:

public static String hexToBinary(String hex) { 
    return new BigInteger(hex, 16).toString(2); 
} 
+0

它增加了額外的位 – 2017-01-23 11:44:40

5

這裏有一些程序我寫操縱十六進制,純文本和二進制,希望他們幫助。因爲我從這些主題中借鑑了一些想法,所以我想分享一下。

public static String zero_pad_bin_char(String bin_char){ 
    int len = bin_char.length(); 
    if(len == 8) return bin_char; 
    String zero_pad = "0"; 
    for(int i=1;i<8-len;i++) zero_pad = zero_pad + "0"; 
    return zero_pad + bin_char; 
} 
public static String plaintext_to_binary(String pt){ 
    return hex_to_binary(plaintext_to_hex(pt)); 
} 
public static String binary_to_plaintext(String bin){ 
    return hex_to_plaintext(binary_to_hex(bin)); 
} 
public static String plaintext_to_hex(String pt) { 
    String hex = ""; 
    for(int i=0;i<pt.length();i++){ 
     String hex_char = Integer.toHexString(pt.charAt(i)); 
     if(i==0) hex = hex_char; 
     else hex = hex + hex_char; 
    } 
    return hex; 
} 
public static String binary_to_hex(String binary) { 
    String hex = ""; 
    String hex_char; 
    int len = binary.length()/8; 
    for(int i=0;i<len;i++){ 
     String bin_char = binary.substring(8*i,8*i+8); 
     int conv_int = Integer.parseInt(bin_char,2); 
     hex_char = Integer.toHexString(conv_int); 
     if(i==0) hex = hex_char; 
     else hex = hex+hex_char; 
    } 
    return hex; 
} 
public static String hex_to_binary(String hex) { 
    String hex_char,bin_char,binary; 
    binary = ""; 
    int len = hex.length()/2; 
    for(int i=0;i<len;i++){ 
     hex_char = hex.substring(2*i,2*i+2); 
     int conv_int = Integer.parseInt(hex_char,16); 
     bin_char = Integer.toBinaryString(conv_int); 
     bin_char = zero_pad_bin_char(bin_char); 
     if(i==0) binary = bin_char; 
     else binary = binary+bin_char; 
     //out.printf("%s %s\n", hex_char,bin_char); 
    } 
    return binary; 
} 
public static String hex_to_plaintext(String hex) { 
    String hex_char; 
    StringBuilder plaintext = new StringBuilder(); 
    char pt_char; 
    int len = hex.length()/2; 
    for(int i=0;i<len;i++){ 
     hex_char = hex.substring(2*i,2*i+2); 
     pt_char = (char)Integer.parseInt(hex_char,16); 
     plaintext.append(pt_char); 
     //out.printf("%s %s\n", hex_char,bin_char); 
    } 
    return plaintext.toString(); 
} 

}

3

接受的答案只適用於32位值,且替代的BigInteger版本的二進制字符串截斷前導零!這是一個可以在任何情況下工作的函數。

public static String hexToBinary(String hex) { 
    int len = hex.length() * 4; 
    String bin = new BigInteger(hex, 16).toString(2); 

    //left pad the string result with 0s if converting to BigInteger removes them. 
    if(bin.length() < len){ 
     int diff = len - bin.length(); 
     String pad = ""; 
     for(int i = 0; i < diff; ++i){ 
      pad = pad.concat("0"); 
     } 
     bin = pad.concat(bin); 
    } 
    return bin; 
} 
+0

它增加了額外的位 – 2017-01-23 11:38:23

0
private static Map<String, String> digiMap= new HashMap<>(); 
static { 
    digiMap.put("0", "0000"); 
    digiMap.put("1", "0001"); 
    digiMap.put("2", "0010"); 
digiMap.put("3", "0011"); 
digiMap.put("4", "0100"); 
digiMap.put("5", "0101"); 
digiMap.put("6", "0110"); 
digiMap.put("7", "0111"); 
digiMap.put("8", "1000"); 
digiMap.put("9", "1001"); 
digiMap.put("A", "1010"); 
digiMap.put("B", "1011"); 
digiMap.put("C", "1100"); 
digiMap.put("D", "1101"); 
digiMap.put("E", "1110"); 
digiMap.put("F", "1111"); 

}

static String hexToBin(String s) { 
    char[] hex=s.toCharArray(); 
    String binaryString=""; 
    for(char h : hex){ 
     binaryString=binaryString+ digiMap.get(String.valueOf(h)); 
    } 

    return binaryString; 
} 

對不起,它已經有點晚了。但是,我認爲我的答案是最直接簡單的答案。

問候, YUN寒喧之後 2018年2月9日

相關問題