安卓将字符串互转⼆进制数组字符串转化为⼆进制数组
String s = "我是⼭东师范⼤学的⼀名学⽣。";
数组转换成字符串byte[] nn = new byte[0];
try {
nn = (s).getBytes("UTF-8");//可更改为Unicode、ASCII编码
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int wlen1= nn.length;
String wm1 ="";
for (int k = 0; k < wlen1; k++) {
byte b = nn[k];
String result = "";
byte a = b;
for (int i = 0; i < 8; i++) {
byte c = a;
a = (byte) (a >> 1);//每移⼀位如同将10进制数除以2并去掉余数。
a = (byte) (a << 1);
if (a == c) {
result = "0" + result;
} else {
result = "1" + result;
}
a = (byte) (a >> 1);
}
wm1 =new StringBuilder(wm1).append(result).toString();
}
char[] CharArray();
int []wm3=new int[wm1.length()];
for (int k = 0; k < wm1.length(); k++) {
wm3[k] =wm2[k]-48;//减去字符0的ascii码
}
⼆进制数组转换成字符串
char []wm5=new char[wm1.length()];
for (int k = 0; k < wm1.length(); k++) {
wm5[k] = (char)(wm3[k]+'0');//
}
String wm4=String.valueOf(wm5);
int size = wm1.length()/8;
//定义接收数组
byte[] bytes1 = new byte[size];
for (int i = 0; i < size; i++) {
//每次截取8位计算
String tmp = wm4.substring(8*i,8*(i+1));
int tmpInt = Integer.parseInt(tmp,2);
byte tmpByte = (byte)(tmpInt & 0xff);
bytes1[i] = tmpByte;
}
String str = new String(bytes1, Charset.forName("UTF-8"));

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。