Java Convert String to Binary

This article shows you five examples to convert a string into a binary string representative or vice verse.

本文向您展示了五个示例,这些示例将字符串转换为二进制字符串表示形式,反之亦然。

  1. Convert String to Binary – Integer.toBinaryString
  2. Convert String to Binary – Bit Masking
  3. Convert Binary to String – Integer.parseInt
  4. Convert Unicode String to Binary.
  5. Convert Binary to Unicode String.

1. Convert String to Binary – Integer.toBinaryString

The steps to convert a string to its binary format.

  1. Convert string to char[].
  2. Loops the char[].
  3. Integer.toBinaryString(aChar) to convert chars to a binary string.
  4. String.format to create padding if need.
package com.mkyong.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StringToBinaryExample01 {

    public static void main(String[] args) {

        String input = "Hello";
        String result = convertStringToBinary(input);

        System.out.println(result);

        // pretty print the binary format
        System.out.println(prettyBinary(result, 8, " "));
    }

    public static String convertStringToBinary(String input) {

        StringBuilder result = new StringBuilder();
        char[] chars = input.toCharArray();

        for (char aChar : chars) {
            result.append(String.format("%8s", Integer.toBinaryString(aChar)).replaceAll(" ", "0"));	// char -> int, auto-cast zero pads
        }
        return result.toString();
    }

    public static String prettyBinary(String binary, int blockSize, String separator) {

        List<String> result = new ArrayList<>();
        int index = 0;
        while (index < binary.length()) {
            result.add(binary.substring(index, Math.min(index + blockSize, binary.length())));	
            index += blockSize;
        }
        return result.stream().collect(Collectors.joining(separator));
    }
}
Output
0100100001100101011011000110110001101111
01001000 01100101 01101100 01101100 01101111

2. Convert String to Binary – Bit Masking.

2.1 This Java example will use bit masking technique to generate binary format from an 8-bit byte.

package com.mkyong.convert;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StringToBinaryExample02 {

    public static void main(String[] args) {

        String input = "a";
        String result = convertByteArraysToBinary(input.getBytes(StandardCharsets.UTF_8));
        System.out.println(prettyBinary(result, 8, " "));
    }

    public static String convertByteArraysToBinary(byte[] input) {

        StringBuilder result = new StringBuilder();
        for (byte b : input) {
            int val = b;
            for (int i = 0; i < 8; i++) {
                result.append((val & 128) == 0 ? 0 : 1);    // 128 = 1000 00000
                val <<= 1;
            }
        }
        return result.toString();
    }

    public static String prettyBinary(String binary, int blockSize, String separator) {

        List<String> result = new ArrayList<>();
        int index = 0;
        while (index < binary.length()) {
            result.add(binary.substring(index, Math.min(index + blockSize, binary.length())));
            index += blockSize;
        }
        
        return result.stream().collect(Collectors.joining(separator));
    }
}

Output

01100001

The hard part is this code. The idea is similar to this Java – Convert Integer to Binary using bit masking. In Java, byte is an 8-bit, int is 32-bit, for integer 128 the binary is 1000 0000.

困难的部分是这段代码。这个想法类似于–使用位掩码将整数转换为二进制。在Java中,字节是8位,整数是32位,对于整数128,二进制是1000 0000。

for (byte b : input) {
    int val = b;                                      // byte -> int
    for (int i = 0; i < 8; i++) {
        result.append((val & 128) == 0 ? 0 : 1);      // 128 = 1000 0000
        val <<= 1;                                    // val = val << 1
    }
}

This & is an Bitwise AND operator, only 1 & 1 is 1, other combinations are all 0.

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

This val <<= 1 is actually val = val << 1, it is a bit left shift operator, it moves the bits to the left side by 1 bit.

Review the following draft: let’s assume the val is an int, or byte represents a character a.

00000000 | 00000000 | 00000000 | 01100001   # val  = a in binary
00000000 | 00000000 | 00000000 | 10000000   # 128
&                                           # bitwise AND
00000000 | 00000000 | 00000000 | 00000000   # (val & 128) == 0 ? 0 : 1, result = 0

00000000 | 00000000 | 00000000 | 11000010   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&                                           # bitwise AND
00000000 | 00000000 | 00000000 | 10000000   # (val & 128) == 0 ? 0 : 1, result = 1

00000000 | 00000000 | 00000001 | 10000100   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 10000000   # result = 1

00000000 | 00000000 | 00000011 | 00001000   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 00000000   # result = 0

00000000 | 00000000 | 00000110 | 00010000   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 00000000   # result = 0

00000000 | 00000000 | 00001100 | 00100000   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 00000000   # result = 0

00000000 | 00000000 | 00011000 | 01000000   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 00000000   # result = 0

00000000 | 00000000 | 00110000 | 10000000   # val << 1
00000000 | 00000000 | 00000000 | 10000000   # 128
&
00000000 | 00000000 | 00000000 | 10000000   # result = 1

# collect all bits                          # 01100001

For string a the binary string is 01100001.

3. Convert Binary to String.

In Java, we can use Integer.parseInt(str, 2) to convert a binary string to a string.

package com.mkyong.crypto.bytes;

import java.util.Arrays;
import java.util.stream.Collectors;

public class StringToBinaryExample03 {

    public static void main(String[] args) {

        String input = "01001000 01100101 01101100 01101100 01101111";

        // Java 11 makes life easier
        String raw = Arrays.stream(input.split(" "))
                .map(binary -> Integer.parseInt(binary, 2))
                .map(Character::toString)
                .collect(Collectors.joining()); // cut the space

        System.out.println(raw);
    }
}

4. Convert Unicode String to Binary.

We can use Unicode to represent non-English characters since Java String supports Unicode, we can use the same bit masking technique to convert a Unicode string to a binary string.

由于Java String支持Unicode,因此可以使用Unicode来表示非英语字符,我们还可以使用相同的位掩码技术将Unicode字符串转换为二进制字符串。

This example converts a single Chinese character (It means you in English) to a binary string.

package com.mkyong.crypto.bytes;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class UnicodeToBinary01 {

    public static void main(String[] args) {

        byte[] input = "你".getBytes(StandardCharsets.UTF_8);
        System.out.println(input.length);	// 3, 1 Chinese character = 3 bytes
        String binary = convertByteArraysToBinary(input);
        System.out.println(binary);
        System.out.println(prettyBinary(binary, 8, " "));
    }

    public static String convertByteArraysToBinary(byte[] input) {

        StringBuffer result = new StringBuffer();
        for (byte b : input) {
            int val = b;
            for (int i = 0; i < 8; i++) {
                result.append((val & 128) == 0 ? 0 : 1);    // 128 = 1000 0000
                val <<= 1;
            }
        }
        return result.toString();
    }

    public static String prettyBinary(String binary, int blockSize, String separator) {
        
        List<String> result = new ArrayList<>();
        int index = 0;
        while (index < binary.length()) {
            result.add(binary.substring(index, Math.min(index + blockSize, binary.length())));
            index += blockSize;
        }
        return result.stream().collect(Collectors.joining(separator));
    }
}

Output

3
111001001011110110100000
11100100 10111101 10100000

Different Unicode requires different bytes, and not all Chinese characters required 3 bytes of storage, some may need more or fewer bytes.

不同的Unicode需要不同的字节,并非所有汉字都需要3个字节的存储空间,有些可能需要更多或更少的字节。

5. Convert Binary to Unicode String.

Read comments for self-explanatory.

package com.mkyong.crypto.bytes;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class UnicodeToBinary02 {

    public static void main(String[] args) {

        String binary = "111001001011110110100000";     // 你, Chinese character
        String result = binaryUnicodeToString(binary);
        System.out.println(result.trim());
    }

    // <= 32bits = 4 bytes, int needs 4 bytes
    public static String binaryUnicodeToString(String binary) {

        byte[] array = ByteBuffer.allocate(4)
                .putInt(Integer.parseInt(binary, 2))    // 4 bytes byte[]
                .array();

        return new String(array, StandardCharsets.UTF_8);
    }
}

Output

原文地址:https://www.cnblogs.com/PrimerPlus/p/13185621.html