Skip to content

Instantly share code, notes, and snippets.

@n00bsys0p
Created June 12, 2014 21:07
Show Gist options
  • Save n00bsys0p/2401a3420f12665a149f to your computer and use it in GitHub Desktop.
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.
<?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