www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Subtracting Hugh Hex Numbers

  1. #1
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887

    Subtracting Hugh Hex Numbers

    Hey, I'm looking for a function in JavaScript that can subtract some Hugh Hexadecimal Numbers from each-other and return a correct result in Hexadecimal, and not decimal nor scientific notation.

    For example "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"
    minus "EFF0ED0F90E"
    That's the kind of function I'm looking for.


    Just to get your brains started, here's some subtraction work in Hex:
    Code:
    			EF9CD4
    	           -	7EB9A3
    
    			70E331
    Thanks for your help,
    anybody?

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Here ya go.

    Code:
    <script type="text/javascript"><!--
    Number.prototype.numToHex = function(){
     return this.toString(16).toUpperCase();
    }
    
    String.prototype.hexToNum = function(){
     return parseInt(this, 16);
    }
    
     var hex1 = "EF9CD4".hexToNum();
     var hex2 = "7EB9A3".hexToNum();
     var hexResult = (hex1-hex2).numToHex();
    
     alert(hexResult);
    
    //--></script>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Scientific Notation pops-out and thus our answer is rounded... The soul-purpose of this function needs to be to subtract hugh numbers from each other.
    I was hoping that I wouldn't need to re-invent the wheel...
    Code:
    <script type="text/javascript"><!--
    Number.prototype.numToHex = function(){
     return this.toString(16).toUpperCase();
    }
    
    String.prototype.hexToNum = function(){
     return parseInt(this, 16);
    }
    
    var hex1 = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709".hexToNum();
    var hex2 = "1".hexToNum();
    var hexResult = (hex1-hex2).numToHex();
    alert(hexResult);//alerts "D.A39A3EE5E6B48()E+39" in IE6.0
    </script>
    Last edited by Ultimater; 04-05-2005 at 01:38 AM.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I think JavaScript has a mathematical limit on how much it can calculate at once. In other words, you've hit a brick wall. Maybe someone else knows more about JavaScript and its limitations...
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Ok, I was just checking that the "wheel" wasn't already invented. Of coarse I could figure this out in a different language, but JavaScript makes "everything" much more convenient.
    Other than the fact that JS doesn't have a big numbers module, like PERL does, I will just need to be the first to make one.

  6. #6
    Join Date
    Mar 2003
    Posts
    575
    I think I see how to do it.

    Why limit it to hex? Why not make a function that takes three arguments: the first number, the second number, and the base? (So you can use it with any base.)

    If I were you, I would look at the strings, character by character (starting from the right), turn the characters into their numeric values, put the numeric values as digits into arrays, and then to subtract them, I would use the method you learned in second grade. I would also check for errors (invalid digits). Also, I would accept both lowercase and capital letters for hex.

    What do you do if the answer comes out negative?

  7. #7
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    If it's negitive, just simply switch arguments[1] with arguments[0] and set the varible NegitiveNumber equal to true then the function knows to return a negitive number.

  8. #8
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    I made this arbitrary-precision integer library a while ago.

    It is able to perform addition, subtraction, multiplication, division, and modulus on silly huge numbers (no non-integer math, sorry) in base 10 (no other bases either, sorry). There's also a FME function in there since I was playing with this to do RSA-style encryption (not really worthwhile in JS, but fun nonetheless)

    Algorithmically it's pretty simple-minded; it basically performs the operations like you would do them on paper. It is also quite possible there are bugs in the code.

    Maybe I'll try adding base-conversion and non-integer capabilities to it some time.
    Attached Files Attached Files
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  9. #9
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Hey, not bad, HaganeNoKokoro!
    I was only expecting it to subtract and maybe, as a bonus, add.
    However, it can only handle base-10....
    Hmm...
    Good Job though

  10. #10
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    so to handle other bases, you just have to make it convert everything to base 10, then convert back
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  11. #11
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    That's when it gets turned into Scientic Notation...

  12. #12
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    here are some functions for base conversion. I haven't thoroughly tested them though, they probably need some work:
    Code:
    //convert num (base up to 16) to base 10
    function toBase10(num, from) {
      var place="1";
      var result="0";
      for(var i=num.length-1; i>=0; i--) {
        var cur=String(num.charAt(i));
    	if(cur=="f") cur="15";
    	if(cur=="e") cur="14";
    	if(cur=="d") cur="13";
    	if(cur=="c") cur="12";
    	if(cur=="b") cur="11";
    	if(cur=="a") cur="10";
    	result=result.add(place.mul(cur));
    	place=place.mul(from);
      }
      return result;
    }
    //convert num (base 10) to bases up to 16
    function toBase(num, to) {
      var n=num;
      var result="";
      while(compare(n, "0")>0) {
        var r=n.mod(to);
    	if(r=="10") r="a";
    	if(r=="11") r="b";
    	if(r=="12") r="c";
    	if(r=="13") r="d";
    	if(r=="14") r="e";
    	if(r=="15") r="f";
    	n=n.div(to);
    	result=r+""+result;
      }
      return result;
    }
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  13. #13
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    I can top those:
    HTML Code:
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    String.prototype.BaseToBase=function(){
    return parseInt(this,arguments[0]).toString(arguments[1])
    }
    alert("F".BaseToBase(16,2))
    </SCRIPT>

  14. #14
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Won't that lose precision with really big numbers?
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  15. #15
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Hey, you're right! It turned the big numbers into Scientic Notation...
    So if we combine your two posts together, we can add big Hex numbers together and still recieve a result in Hex!
    Nice!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles