Number Base Converter in JavaScript : Number Base Converter « Security « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Security » Number Base Converter 
Number Base Converter in JavaScript

<html>
  <head>
    <!--
      CryptoMX Tools
      Copyright (C2004 2006 Derek Buitenhuis

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your optionany later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    -->
    <title>Number Base Converter</title>
  </head>
  <body>
    <script language=JavaScript>
      <!--calc
      function doBaseCon() {
        form=document.forms[0];
        from=form.from.value;to=form.to.value;val=""+form.val.value;

        if(check(to,0)==|| check(from,0)==1){form.res.value="Bases must be integer (excluding -1, 0, +1).";return;}
        if(to==|| from==0){form.res.value="That base is a mathematical impossibility.";return;}
        if(to==-|| to==|| from==-|| from==1){form.res.value="I don't think that base will work so well.";return;}

        from=1*from;to=1*to;

        txt=baseCon(from,to,val);

        form.res.value=txt;

      }
      function check(n,dec) {
        np=0;e=0;l=n.length;
        for(r=0;r<l;r++) {
          c=n.substring(r,r+1);
          if(c<"0" || c>"9") {
            e2=1;
            if(c=="." && np==&& dec==1){np++;e2=0;}
            if((c=="+" || c=="-")&& r==&& l>1){e2=0}
            e=e | e2;
          }
        }if(n=="-." || n=="+."return 1;
        return e;
      }

      function clr() {
        f=document.forms[0];
        f.res.value="";
      }

      function format(num) {
        if(""+num=="NaN"){return "Check that input."}

        txt="";
        if(num<0){txt+="-"; num*=-1}
        for(n=0; n<11; n++) {
          dig=Math.floor(num);
          num=(num-dig)*10;
          txt+=dig;
          if(n==0)txt+=".";
        }
        return txt;
      }

      function baseCon(from,to,val) {
        val2=baseXCon10(from,val);
        if(""+val2=="nh")sum="No alpha-numerics in base "+from+" numbers."else sum=base10ConX(to,val2);
        return sum;

      }
      function baseXCon10(from,val){
        val=""+val;sum=0;sgn=1;pl=0;div=1;
        for(a=0;a<val.length;a++) {
          ch=val.substring(a,a+1);
          if(ch=="+" && a==0)sgn=1;
          else {
           if(ch=="-" && a==0)sgn=-1;
           else {
            if(ch==".")div=pl;
            else {
            if(ch>='0' && ch<='9')dg=1*ch;
            ch=ch.toUpperCase();
            if(ch=='A')dg=10;if(ch=='B')dg=11;if(ch=='C')dg=12;if(ch=='D')dg=13;if(ch=='E')dg=14;if(ch=='F')dg=15;
              if(ch>='A' && ch<='Z' && from<=10 && from>0) {nh="nh"return nh;}
            sum+=Math.pow(from,pl)*dg;
            pl--;
            }
          }
        }

        }
        if(div==1)div=pl;
        sum/=Math.pow(from,div+1);
      if(sum>1000000 && sum-Math.floor(sum)<.000001sum=Math.floor(sum);
      if(sum>1000000 && Math.ceil(sum)-sum<.000001sum=Math.ceil(sum);
        sum*=sgn; round=1000000000000
        sum=Math.floor(sum*round+.5)/round;
        return sum;
      }
      function base10ConX(to,val) {
        //to new
        if(val<0){sgn="-";val=-val;}else{sgn="";}
        sum="";

        c=Math.floor(val);

        start=0;rem=0;digits=0;
        if(c==0)sum="0";
        while(Math.abs(c)>=1) {
          d=c-Math.floor(c/to)*to;
            dfl=Math.floor(d);
            if(dfl!=d){rem=d-dfl;d=dfl;}
          if(Math.abs(d)>Math.abs(to)) d=0;
          c=Math.floor(c/to);
          if((start==&& d!=0|| c<val*to)start=1;start=1;
            sum=""+alp(d,to)+sum;
          digits++;

        }
        c=val-Math.floor(val)+rem;
        if(c!=0) {//decimals
          sum+=".";
          for(a=0;a<32/Math.log(to)-digits;a++) {
            if(c==0)break;
            d=Math.floor(c*to);
            if(to<&& d==tobreak;
            c=c*to-d;
            sum+=alp(d,to);
          }
        }
        sum=sgn+sum;

        return sum;
      }
      function alp(d,to) {
        if(d>=&& d<=9)w=""+d;
        if(to>=10 && to<=36) {
          if(d==10)w="A";if(d==11)w="B";if(d==12)w="C";if(d==13)w="D";if(d==14)w="E";if(d==15)w="F";if(d==16)w="G";if(d==17)w="H";if(d==18)w="I";if(d==19)w="J";if(d==20)w="K";if(d==21)w="L";if(d==22)w="M";if(d==23)w="N";if(d==24)w="o";if(d==25)w="P";if(d==26)w="Q";if(d==27)w="R";if(d==28)w="S";if(d==29)w="T";if(d==20)w="U";if(d==31)w="V";if(d==32)w="W";if(d==33)w="X";if(d==34)w="Y";if(d==35)w="Z";
        }
        if(d<|| d>35 || (d>&& to>36))
          w="["+d+"]";
        return w;
      }

      //-->
    </script>
    <td>&nbsp;&nbsp;</td>
    <td vAlign=top>
      <p><font color=#000000 size=5><b>Number Base Converter</b></font></p>
      <p><font color=#000000 size=3>Convert numbers from one base to another.</font></p>
      <table cellSpacing=cellPadding=border=1>
        <form name=inp>
        <tbody>
        <tr bgColor=#ffffff>
          <td>from base</td>
          <td>to base</td>
          <td>value to convert</td></tr>
        <tr bgColor=#ffffff>
          <td><input onmousedown=clr() size=value=10 name=from> </td>
          <td><input onmousedown=clr() size=value=16 name=to> </td>
          <td><input onmousedown=clr() size=50 value=256 name=val> </td></tr>
        <tr bgColor=#ffffff>
          <td><input onclick=doBaseCon() type=button value=calculate name=button>
          </td>
          <td colSpan=2><input size=50 name=res> </td></tr></form></tbody></table>
          </td>
    </tr>
  </body>
</html>
           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.