进制
- 进制转换
- 十制转换为其他进制:
Integer.toBinaryString(int i)
,静态方法转化,还有Integer.toHexString(int i)
,Integer.toOctalString(int i)
- 其他进制转化为十进制,
Integer.parsenInt(String string,int num)
()String为要转化的数字对应的字符串,num为标识String是多少进制。
- 十制转换为其他进制:
- 数据类型 转化 字节:
- int,long:采用大小端法,(小端法)低位字节排放在内存的低地址端即该值的起始地址,(大端法)高位字节排放在内存的低地址端即该值的起始地址
/**
*int 转化为byte[]
*/
byte[] arr = new byte[];
int n = 8143;
for(int i ;i < 4;i++){
arr[i] = (byte)(int)((n >> i*8) & 0xff);
}
//byte 数组存储的就是int对应的字节,小端法。
//int-32位-4字节 long-64位-8字节
int result = 0;
for(int i = 0;i < 4; i++){
result += (int)((arr[i] & 0xff) << i*8);
}
return result;
- String:采用String内的方法
String s;
byte[] arr = s.getBytes();
byte[] arr; //已经将byte传到arr
String s = new String(arr)
//或者String s = new String(arr,encode)
//encode是编码方式“gb2312,utf-8”
- int,long:采用大小端法,(小端法)低位字节排放在内存的低地址端即该值的起始地址,(大端法)高位字节排放在内存的低地址端即该值的起始地址