Ich habe eine sehr große Folge von Dezimalzahlen. Wie konvertiere ich sie in eine Binärzeichenfolge?
Da die Zahl sehr groß ist, gibt es keine Möglichkeit, sie in eine Zahl umzuwandeln und in die Variable toString(2) einzufügen
按照十进制转二进制的原理,自己模拟除2取余
一种自己写算法 应该不会太难 另一种 交给后台处理
private static String process(ArrayList<String> imp,String HexStr){ String rem = ""; Long remaind = (long) 0; ArrayList<String> newList = new ArrayList<String>(); Iterator<String> itor = imp.iterator(); while(itor.hasNext()){ String ele = itor.next(); Long num = Long.parseLong(rem+ele); Long quot = num / 16; remaind = num%16; rem = remaind.toString(); if(quot > 0){ newList.add(padString(quot.toString(),16,"0",0)); } }; String hexRem = Long.toHexString(remaind); if(newList.isEmpty()){ return hexRem; } else{ return process(newList,hexRem)+hexRem; } } public static String D2Hex(String dNum){ String tmp = dNum; ArrayList<String> splitNum = new ArrayList<String>(); while(tmp.length() > 16){ String high = tmp.substring(0,tmp.length() - 16); splitNum.add(high); String low = tmp.substring(tmp.length() - 16); tmp = low; } splitNum.add(tmp); return process(splitNum,""); } public static String D2B(String dNum){ String hex = D2Hex(dNum); StringBuilder bin= new StringBuilder(); for(int i =0; i < hex.length(); i ++){ String tmp = Integer.toBinaryString( Integer.parseInt(hex.substring(i,i+1),16)); bin.append(padString(tmp,4,"0",0)); } return bin.toString(); }
超大数结果未验证 0------>0--->0000135,9234------>14bd82--->0001010010111101100000109999,9999,9999,9999------>2386f26fc0ffff--->001000111000011011110010011011111100000011111111111111111,0000,0000,0000,0000------>2386f26fc10000--->001000111000011011110010011011111100000100000000000000009,9999,9999,9999,9999------>16345785d89ffff--->0001011000110100010101111000010111011000100111111111111111119999,9999,9999,9999,9999,9999,9999,9999------>4ee2d6d415b85acef80ffffffff--->010011101110001011010110110101000001010110111000010110101100111011111000000011111111111111111111111111111111
随便写一个吧
function toBin(str) { var arr = []; var remainder,i,str2,num,char; while(str.length>0) { str2 = "";remainder=0; for(i=0;i<str.length;i++) { // str2 = str组成的十进制数 / 2 num = str.charCodeAt(i)-0x30; // num to String num = remainder*10 + num; char = Math.floor(num/2).toString(); // 忽略最高为的0 , 即最高为如果是 0 则不放入 str2 if(!(char === "0" && str2 === "")) { str2 += char;} remainder = num%2; } str = str2; arr.push(remainder); // 保存余数 } return arr.reverse().join(''); } console.log(toBin("3")); // 11 console.log(toBin("9")); // 1001 console.log(toBin("10")); // 1010 console.log(toBin("120")); // 1111000
我想问,超大,有多大
如果只是显示的话, 可以考虑让后台转
按照十进制转二进制的原理,自己模拟除2取余
一种自己写算法 应该不会太难 另一种 交给后台处理
超大数结果未验证
0------>0--->0000
135,9234------>14bd82--->000101001011110110000010
9999,9999,9999,9999------>2386f26fc0ffff--->00100011100001101111001001101111110000001111111111111111
1,0000,0000,0000,0000------>2386f26fc10000--->00100011100001101111001001101111110000010000000000000000
9,9999,9999,9999,9999------>16345785d89ffff--->000101100011010001010111100001011101100010011111111111111111
9999,9999,9999,9999,9999,9999,9999,9999------>4ee2d6d415b85acef80ffffffff--->010011101110001011010110110101000001010110111000010110101100111011111000000011111111111111111111111111111111
随便写一个吧
我想问,超大,有多大
如果只是显示的话, 可以考虑让后台转