Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimals, javascript vs C#

I am trying to convert a JavaScript hashing function to C# hashing to do the exact same thing. I'm 99% there but I hit a snag with decimals used in this custom function.
Am not sure why but this function convert a hashed value to a decimal for some odd reason and my problem is that decimals generated are not always the same length. The decimals in C# are quite a bit longer but are uniform length. The problem i am having is because rounding in C# works differently than JavaScript I don't know exactly at what decimal to round to create the equivalent length string.

Here is an example of two generated decimal strings that are appended to each other. Both start from 4,4 and 3 character strings:

4 char string generates 79957.88183577501
4 char string generates 160933.02806113224
3 char string generates 609.9111294990053

Using the exact same code C# generates using the exact same inputs:

79957.88183577500452161331162
160933.02806113221197323204919
609.91112949900524507144149035

If all strings were the same length it would not be a problem but I have no idea how to determine when JS will generate the longer decimal. Any clues? Comments? Opinions?

Unfortunately the receiving code is still the original JS which simply reverses the process hence I have to duplicate the end result perfectly for all inputs.

EDIT:

Here is the problematic section. Don't ask me why it works like that, I didn't write it.

// oString is a full string to be encoded
// oKey is a key to be used for encoding
function completeHash(oString,oKey) {
    if( oKey.length < 5 ) {
        window.alert( 'The key must be at least 5 characters long' );
        return oString;
    }
    var oKeyNum = new Array(), oOutStr = '', oOp = new Array('+=','/=','-=','*= 0.01 *');
    for (var x = 0; x < oKey.length; x++) {
        oKeyNum[x] = parseInt('0x' + completeEscape(oKey.charAt(x)));
    }

    for( var x = 0, y = ''; x < oString.length; x += Math.round( oKey.length / 2 ), y = 'O.O' ) {
        var theNum = parseInt( '0x' + completeEscape( oString.substr( x, Math.round( oKey.length / 2 ) ) ) );

        // next two lines are problematic with decimals not having equal length
        for( var z = 0; z < oKey.length; z++ ) {
            eval( 'theNum ' + oOp[z % 4] + ' ' + oKeyNum[z] + ';' );
            alert('theNum:' + theNum);
        }

        oOutStr += y + theNum;
    }
    return oOutStr;
}

completeEscape() function simply returns ASCII int codes for each character.

I got the whole thing working nicely except the length of the decimals.

like image 220
IcantSpell Avatar asked Oct 23 '12 22:10

IcantSpell


1 Answers

If you're using Number in javascript, then use double in C#. Both are 64-bit IEEE 754 numbers (double-precision). You get the same values (updated after verifying this).

like image 124
Peter Ivan Avatar answered Sep 30 '22 09:09

Peter Ivan