What are the equations to convert between bits and difficulty? If we take block hash 0000000000000006770c3806960539ca83a24facbd99ea212f37f2a0e6a5629a for example.

The difficulty as a 32 bit float is 50810339.04827648. The difficulty in bits as an unsigned 32 bit integer is 424970034?

What are the equations to go from difficulty -> bits and bits -> difficulty?
Answer shamelessly stolen from stackexchange:


  Difficulty encoding is thoroughly described
  here.
  
  Hexadecimal representation like 0x182815ee consists of two parts:
  
  
  0x18 -- number of bytes in a target
  0x2815ee -- target prefix
  
  
  This means that valid hash should be less than
  0x2815ee000000000000000000000000000000000000000000 (it is exactly
  0x18 = 24 bytes long).
  
  Floating point representation of difficulty shows how much current
  target is harder than the one used in the genesis block.
  
  Satoshi decided to use 0x1d00ffff as a difficulty for the genesis
  block, so the target was 
  0x00ffff0000000000000000000000000000000000000000000000000000.
  
  And 50810339.04827648 is how much current target is greater than the
  initial one.


How the Bitcoin client converts from bits -> difficulty:

uint256&amp; uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
{
    int nSize = nCompact &gt;&gt; 24;
    uint32_t nWord = nCompact &amp; 0x007fffff;
    if (nSize &lt;= 3) {
        nWord &gt;&gt;= 8*(3-nSize);
        *this = nWord;
    } else {
        *this = nWord;
        *this &lt;&lt;= 8*(nSize-3);
    }
    if (pfNegative)
        *pfNegative = nWord != 0 &amp;&amp; (nCompact &amp; 0x00800000) != 0;
    if (pfOverflow)
        *pfOverflow = nWord != 0 &amp;&amp; ((nSize &gt; 34) ||
                                     (nWord &gt; 0xff &amp;&amp; nSize &gt; 33) ||
                                     (nWord &gt; 0xffff &amp;&amp; nSize &gt; 32));
    return *this;
}


How the Bitcoin client converts from difficulty -> bits:

uint32_t uint256::GetCompact(bool fNegative) const
{
    int nSize = (bits() + 7) / 8;
    uint32_t nCompact = 0;
    if (nSize &lt;= 3) {
        nCompact = GetLow64() &lt;&lt; 8*(3-nSize);
    } else {
        uint256 bn = *this &gt;&gt; 8*(nSize-3);
        nCompact = bn.GetLow64();
    }
    // The 0x00800000 bit denotes the sign.
    // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
    if (nCompact &amp; 0x00800000) {
        nCompact &gt;&gt;= 8;
        nSize++;
    }
    assert((nCompact &amp; ~0x007fffff) == 0);
    assert(nSize &lt; 256);
    nCompact |= nSize &lt;&lt; 24;
    nCompact |= (fNegative &amp;&amp; (nCompact &amp; 0x007fffff) ? 0x00800000 : 0);
    return nCompact;
}


Converting from target to difficulty, in shell. Create file target-to-difficulty.sh:

#!/bin/bash
echo "ibase=16;FFFF0000000000000000000000000000000000000000000000000000 / $1" | bc -l


Usage:

$ ./target-to-difficulty.sh 000000000000000024DBE9000000000000000000000000000000000000000000
29829733124.04041574884510759883