How to generate (in JavaScript or Node.JS) float in range from 0.00000000 - 100.00000000 from given long HEX hash? For example SHA-256?
I am open to solutions with crypto
library because I using it to generate given hash :)
How to generate (in JavaScript or Node.JS) float in range from 0.00000000 - 100.00000000 from given long HEX hash? For example SHA-256?
I am open to solutions with crypto
library because I using it to generate given hash :)
If you're not concerned about precision loss, and your hex strings are of a fixed length (as with SHA-256), you could simply map from one value space to the other:
function hexStrToFraction(hexStr) {
// Expresses a hexadecimal string of N characters as a fraction
// of one above its maximum possible value (16^N).
// Returns a number between 0 and 1.
return parseInt(hexStr, 16) / Math.pow(16, hexStr.length);
}
function sha256ToPercent(sha256) {
return 100 * hexStrToFraction(sha256);
}
Note that the precision loss is high enough to render the majority of a SHA-256 redundant:
var a = 'b2339969703a8c4b49e4a5c99cca6013ed455f52d06f8f03adb927aee7d9c3c0'
var b = 'b2339969703a8c8b8504772b860b9ed2cb6aa0186ff6750981e7ccd5344e4bf1'
// ^ differences start here
hexStrToFraction(a) === hexStrToFraction(b) // evaluates true