字符串转换为二进制

public class StringToBinay
{

public static void main(String[] args)
{


String str="ABC12345";

System.out.println(toBinary(str));
}

public static String toBinary(String str)
{

char[] strChar = str.toCharArray();

int [] a=new int[str.length()];
for(int i=0;i<str.length();i++)
{
a[i]=strChar[i];
}

String result = "";
for(int i = 0; i < strChar.length; i++)
{
if(Integer.toBinaryString(a[i]).length()<7)
{
result+="0"+Integer.toBinaryString(a[i])+" ";
}
else
{
result += Integer.toBinaryString(a[i]) + " ";
}
}
return result;
}
}

posted @ 2021-06-14 11:14  莫过无忧  阅读(1459)  评论(0)    收藏  举报