Java中的二进制字符串到ASCII 6bit char

时间:2014-11-05 18:15:47

标签: java

我有一个48字节长的Java字符串。该字符串将被分为8个字符串,长度为6位。每个字符串都应转换为ASCII字符。因此,应使用Sixbit ASCII代码。

现在我有一个包含所有可能的字符表和二进制代码的表。我的第一个想法是通过使用switch case将二进制字符串转换为char,并为每种可能性定义规则,但这不是最好的选择。

是否有某种功能,我可以使用它来自动转换它,而且我不必用开关编写方法?

3 个答案:

答案 0 :(得分:0)

public byte sixBitFromAscii(char asciiChar) {
    if (asciiChar >= 0x60) {
        System.out.println("Invalid character " + asciiChar);
        return 0;
    }
    else {
        return (byte)(asciiChar - 0x20);
    }
}

public char asciiFromSixBit(byte sixBit) {
    return (char) (sixBit + 0x20);
}

答案 1 :(得分:0)

好的,由于澄清,并且您发布了实际的表格,这变得非常简单。在charset排序后,我们可以将目录和索引转换为数组。请记住,如果你输入的是一个0/1的字符串,你必须做一些小事来获取值(在这段代码中命名为n)。否则,它会是一样的。

public class sixbit {
    static final char[] CHARSET = 
        {'@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
        'P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^',
        '_',' ','!','"','#','$','%','&','\'','(',')','*','+',',','-',
        '.','/','0','1','2','3','4','5','6','7','8','9',':',';','<',
        '=','>','?'};

    public static void main(String[] args) {
        // Sample String of length 48, maybe up of 1s and 0s
        String input = "010011010100000001000011001011001111010110010010" ;
        System.out.println(input);
        String[] parts = splitInput(input); // Split into 6-bit pieces
        for(String sixbit: parts) {
            int n = Integer.parseUnsignedInt(sixbit, 2);
            System.out.printf("%s -> %c \n", sixbit,CHARSET[n]);        
        }       
    }

    private static String[] splitInput(String input) {
        String[] parts = new String[8]; // 48 bits, 6 bits each means we get 8 characters;
        int current_part = 0;
        int current_bit = 0;
        StringBuilder sb; 
        while(current_bit < 48) {
            sb = new StringBuilder();
            for(int i=0; i < 6; i++) {
                sb.append(input.charAt(current_bit));               
                current_bit++;
            }
            parts[current_part] = sb.toString();
            current_part++;                     
        }
        return parts;
    }
}

旧版

除了loadLookupTable()方法之外,只包括一些随机扔在一起的表项,这应该做你想要的。

import java.util.*;

public class sixbit {
    static Map<String,Character> lookup = new HashMap<String,Character>();

    public static void main(String[] args) {
        loadLookupTable();  
        // Sample String of length 48, maybe up of 1s and 0s
        String input = "111000111001100110101000110000110100111011110111" ;
        System.out.println(input);
        String[] parts = splitInput(input); // Split into 6-bit pieces
        for(String sixbit: parts) {
            char ch = lookup.get(sixbit); // Lookup each 6-bit String to get the corresponding character.
            System.out.printf("%s -> %c \n", sixbit, ch);
        }

    }

    private static String[] splitInput(String input) {
        String[] parts = new String[8]; // 48 bits, 6 bits each means we get 8 characters;
        int current_part = 0;
        int current_bit = 0;
        StringBuilder sb; 
        while(current_bit < 48) {
            sb = new StringBuilder();
            for(int i=0; i < 6; i++) {
                sb.append(input.charAt(current_bit));               
                current_bit++;
            }
            parts[current_part] = sb.toString();
            current_part++;                     
        }
        return parts;
    }

    private static void loadLookupTable() {
        /* For each bit string you have in your table, add the corresponding character. It would be shorter code,
         * and a touch faster to load this from an array, but it would take a bit of thought and wouldn't be as clear.
         * Grab enough to work for this example, so that this program works. Just need to make sure the full lookup is loaded
         * properly.
         */
        lookup.put("100110", 'a');
        lookup.put("100111", 'b');
        lookup.put("101000", 'c');
        lookup.put("110000", 'k');
        lookup.put("110100", 'o');
        lookup.put("110111", 'r');
        lookup.put("111000", 's');
        lookup.put("111001", 't');
        // and so on...
        lookup.put("111011", 'v');
        lookup.put("111100", 'w');
        lookup.put("111101", 'x');
        lookup.put("111110", 'y');
        lookup.put("111111", 'z');
    }
}

答案 2 :(得分:-1)

将字符串分成字节(每个字节为6位,填充2位)。然后使用一个数组将字节值映射到ASCII char值。

编辑好的,当你有原始的二进制数据时,我误解了你的问题。显然你有一个1和0的字符串,比如&#34; 1010111&#34;长度为48.实际执行情况非常不同(而且更容易)。看到我的其他答案。对不起,感到困惑。