在java中怎么把字节数组转换成二进制字符串

时间:2023-01-11 12:56:22
public class Test {
	public static void main(String args[])   {
		String ZERO="00000000";
		String x ="lalala";
		byte[]   bs   =   x.getBytes();
		for   (int i = 0;i<bs.length;i++)   {
			String   s   =   Integer.toBinaryString(bs[i]);
			if   (s.length()   >
			8)   {
				s   =   s.substring(s.length()   -   8);
			}   else   if   (s.length()  
					<   8)   {
				s   =   ZERO.substring(s.length())   +   s;
			}
			System.out.println(s);
		}
	}
}