- public class TransBinary {
- public static void main(String[] args){
- String arr = transBinary(-6,1,1);
- System.out.println(arr);
-
- }
- public static String transBinary(int num,int offset,int count){
- if(num == 0){
- System.out.println(0);
- return null;
- }
- char[] arr = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',};
- char[] arrs = new char[32];
- int pos = arrs.length;
- int times = 0;
- while(num != 0){
- int temp = num & count;
- arrs[--pos] = arr[temp];
- num = num >>> offset;
- times++;
- }
- return new String(arrs,pos,times);
- }
- }
复制代码 pos应该等于arrs的长度
new String(char[] ch,int offsite,int length)最后一个参数是长度 |