其他分享
首页 > 其他分享> > 字符串转换为二进制

字符串转换为二进制

作者:互联网

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;
}
}

标签:转换,String,二进制,strChar,int,length,result,str,字符串
来源: https://www.cnblogs.com/moaying/p/14882102.html