javascript - js large number to binary
仅有的幸福
仅有的幸福 2017-05-19 10:14:41
0
6
610

I have a very large string of decimal numbers. How to convert it into a binary string?

Because the number is very large, there is no way to convert it into a number and put it in the variable toString(2)

仅有的幸福
仅有的幸福

reply all (6)
某草草

According to the principle of converting decimal to binary, simulate dividing by 2 and taking remainder

    巴扎黑

    It shouldn’t be too difficult to write the algorithm yourself, and leave it to the background for processing

      过去多啦不再A梦
      private static String process(ArrayList imp,String HexStr){ String rem = ""; Long remaind = (long) 0; ArrayList newList = new ArrayList(); Iterator 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 splitNum = new ArrayList(); 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(); }

      Super large number result not verified
      0------>0--->0000
      135,9234------>14bd82--->000101001011110110000010
      9999,9999,9999,9999 ------>2386f26fc0ffff--->0010001110000110111100100110111111 0000001111111111111111
      1,0000,0000,0000,0000------>2386f26fc10 000--->0010001110000110111100100110111111000001000000000000000
      9,9999,9999,9999,9999 ------>16345785d89ffff--->000101100011010001010111100001011101100010011111111111111111
      9999,9999,9999,9999,9999,9999,99 99,9999------>4ee2d6d415b85acef80ffffffff--->01001110111000101101011011010100000101011011100001011010110011101111100000001111 1111111111111111111111111111

        刘奇

        Write whatever you want

        function toBin(str) { var arr = []; var remainder,i,str2,num,char; while(str.length>0) { str2 = "";remainder=0; for(i=0;i
          伊谢尔伦

          I want to ask, super big, how big is it

            为情所困

            If it is just for display, you can consider letting it be transferred in the background

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!