# Thread: Subtracting Hugh Hex Numbers

1. ## 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```
anybody?

2. 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();

//--></script>```

3. 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();
</script>```
Last edited by Ultimater; 04-05-2005 at 01:38 AM.

4. 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...

5. 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. Registered User
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. 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. 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.

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. so to handle other bases, you just have to make it convert everything to base 10, then convert back

11. That's when it gets turned into Scientic Notation...

12. 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";
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;
}```

13. I can top those:
HTML Code:
```<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
String.prototype.BaseToBase=function(){
return parseInt(this,arguments[0]).toString(arguments[1])
}
</SCRIPT>```

14. Won't that lose precision with really big numbers?

15. 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!