欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

超级(无限)大的 --- 整型10进制数据与16进制数据相互转换

程序员文章站 2022-03-14 16:05:14
...
<html>
<title>超级(无限)大的 --- 整型10进制数据与16进制数据相互转换</title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="数据压缩算法,超过整数表示范围的大数据算术运算,大进制数据互相转换" />
<meta name="keywords" content="高效的乘除法竖式计算模型,乘法转换成加法运算,除法转换成减法运算" />
<meta name="keywords" content="原创作者:[email protected]" />
<script type="text/javascript">


function test16to10(){
   var arr= document.getElementsByTagName("input");
   for(var i=0;i<arr.length;i++) {
      if((arr[i].type == "text" )&&(arr[i].name == "A0116")){
       document.getElementsByName("A0110")[0].value = convertBig16ToInt(arr[i].value); 
       //parseInt(arr[i].value,16).toString(10);
     }
   }
}


function test10to16(){
   var arr= document.getElementsByTagName("input");
   for(var i=0;i<arr.length;i++){
      if((arr[i].type == "text" )&&(arr[i].name == "B0110")){
        document.getElementsByName("B0116")[0].value = convertBigIntTo16(arr[i].value);
        //parseInt(arr[i].value,10).toString(16);
     }
   }
}


function convertBigIntTo16(data){
   data = data.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '');   
   var dataLength = data.length;
   if(isNaN(data) || dataLength == 0) return '';
   var yuShu = 0;
   var shang = 0;
   var x = 0;
   var index = 0;
   var yuShuArr = new Array();
   var shangArr = new Array();
   while (dataLength > 1){
       yuShu = 0;
       shang = 0;
       x = 0;
       index = 0;
       shangArr = [];
       while(index < dataLength){
           while(index < dataLength){
              x = yuShu * 10 + parseInt(data.substr(index,1)); 
              yuShu = x;
              if(x > 15){
                index = index + 1;  
                break;
              }else if((index > 1)&&(index + 1 != dataLength)){
                shangArr.push('0');
              }  
              index = index + 1;                
           }
           shang = parseInt(x/16);
           yuShu = x % 16;
           shangArr.push(shang.toString());
        }   
       yuShuArr.push(yuShu.toString());
       data = shangArr.join('');
       dataLength = data.length;
   }
   
   if (dataLength == 1) yuShuArr.push(data);
   for(var i = 0; i < yuShuArr.length; i++){
      if (yuShuArr[i] == 10)
        yuShuArr[i] = "a";
      else if (yuShuArr[i] == 11)
        yuShuArr[i] = "b";
      else if (yuShuArr[i] == 12)
        yuShuArr[i] = "c";
      else if (yuShuArr[i] == 13)
        yuShuArr[i] = "d";
      else if (yuShuArr[i] == 14)
        yuShuArr[i] = "e";
      else if (yuShuArr[i] == 15)
        yuShuArr[i] = "f";
   }
   if(yuShuArr[yuShuArr.length -1] == '0') 
      yuShuArr.pop();
   var result = yuShuArr.reverse().join('');
   return result;
}


function convertBig16ToInt(data){
   data = data.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, ''); 
   var dataLength = data.length;
   if (dataLength == 0) return '';
   var index = 0;
   var x = 0;
   var geArr = new Array();
   var geTohighArr = new Array();
   var shiTohighArr = new Array(); 
   var yuShuArr  = new Array();
   var resultArr  = new Array();
   resultArr[0] = '0'; 
   var arr16 = new Array();
   arr16['0'] = '0';
   arr16['1'] = '1';
   arr16['2'] = '2';
   arr16['3'] = '3';
   arr16['4'] = '4';
   arr16['5'] = '5';
   arr16['6'] = '6';
   arr16['7'] = '7';
   arr16['8'] = '8';
   arr16['9'] = '9';
   arr16['a'] = '10';
   arr16['b'] = '11';
   arr16['c'] = '12';
   arr16['d'] = '13';
   arr16['e'] = '14';
   arr16['f'] = '15';
   while(index < dataLength){
       geArr = resultArr; 
       resultArr = [];
       geTohighArr = [];     
       for(var i=0;i<geArr.length;i++){
         x = 0;
         x = parseInt(i < geArr.length ? geArr[i] : '0') * 6;
         x = x + parseInt(i < geTohighArr.length ? geTohighArr[i] : '0');
         geTohighArr[i] = x % 10;
         if(x > 0)
         geTohighArr[i+1] = parseInt(x / 10);
       }
       shiTohighArr = [];    
       geArr.unshift(0);
        for(var i=0;i<geArr.length;i++){
         x = 0;
         x = parseInt(i < geArr.length ? geArr[i] : '0');
         x = x + parseInt(i < geTohighArr.length ? geTohighArr[i] : '0');
         x = x + parseInt(i < shiTohighArr.length ? shiTohighArr[i] : '0');
         shiTohighArr[i] = x % 10;
         if(x > 0)
            shiTohighArr[i+1] = parseInt(x / 10);
       }
       geArr = [] ;
       yuShuArr = [];
       yuShuArr  = arr16[data.substr(index,1)].split('').reverse();
       for(var i=0;i<shiTohighArr.length;i++){
         x = 0;
         x = x + parseInt(i < shiTohighArr.length ? shiTohighArr[i] : '0'); 
         x = x + parseInt(i < yuShuArr.length ? yuShuArr[i] : '0');
         x = x + parseInt(i < resultArr.length ? resultArr[i] : '0');
         resultArr[i] = x % 10;
         if(x > 9)
            resultArr[i+1] = parseInt(x / 10);
       }
     index = index + 1;
   }
   if(resultArr[resultArr.length -1] == 0) 
      resultArr.pop();
   var result = resultArr.reverse().join('');
   return result;
}


</script>
<head>
<body> 
  <div><b>十进制数:16001600016                       十六进制数:3b9c50a10                </b></div><br>
  <div><b>十进制数:1600160000                         十六进制数:5f608100                   </b></div><br>
  <div><b>十进制数:12345678901234567890  十六进制数:ab54a98ceb1f0ad2 </b></div><br>
  <div><b>十进制数:12662001107270911155  十六进制数:afb876ebc07c44b3 </b></div><br>
  <div><b>十进制数:4721702174050929320    十六进制数:4186db1c537ecaa8 </b></div><br>
  <div><b>十进制数:16279334710358849629  十六进制数:e1ebd0aadea0385d </b></div><br>
  <div><b>十进制数:14499311425014492826  十六进制数:c937e6eb9fc06e9a  </b></div><br>
  <div><b>十进制数:18379191081977372545  十六进制数:ff1000e97449df81    </b></div><br>
  <div><b>十进制数:3228213681276039511    十六进制数:2ccceb3d4383b157  </b></div><br>
  <div id="xy">
    <div><b>十六进制数转换成十进制数</b></div><br> 
    十六进制:<input type="text" name="A0116" /> 十进制数:<input type="text" name="A0110" readonly="readonly"/><br> 
    <input type="button" value="开始转换" onclick="test16to10()" /><br><br>
  </div>
  <div id="xyz">
    <div><b>十进制数转换成十六进制数</b></div><br> 
    十进制数:<input type="text" name="B0110" /> 十六进制:<input type="text" name="B0116" readonly="readonly"/><br> 
    <input type="button" value="开始转换" onclick="test10to16()" />
 </div>
</body>
</html>