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:
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>
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.
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.
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.
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.
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.
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
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
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.
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!
Bookmarks