Created
June 12, 2014 21:07
-
-
Save n00bsys0p/2401a3420f12665a149f to your computer and use it in GitHub Desktop.
A quick, easily portable PHP script to convert nBits to an actual diff.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Try this with 1d00ffff for a diff of 1 (original Bitcoin mainnet and testnet) | |
// or 207fffff for 4.6565423739069E-10 (Bitcoin regtest network) | |
function ConvertBitsToDouble($nBits){ | |
$nShift = ($nBits >> 24) & 0xff; | |
$dDiff = | |
(double)0x0000ffff / (double)($nBits & 0x00ffffff); | |
while ($nShift < 29) | |
{ | |
$dDiff *= 256.0; | |
$nShift++; | |
} | |
while ($nShift > 29) | |
{ | |
$dDiff /= 256.0; | |
$nShift--; | |
} | |
return $dDiff; | |
} | |
$nBits_bitcoin = 545259519; | |
echo ConvertBitsToDouble($nBits_bitcoin) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment