user30017: Im trying to understand how to obtain the p2SH address when decoding an output script.
r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));

source
As far as I understand CScriptID is the hash160 of the script.  

user2306: So, a P2SH scriptPubKey looks like this:
OP_HASH160 <20 byte script hash> OP_EQUAL

Serialized, in hex, that looks like:
17a9140000000000000000000000000000000000000087

^ ScriptPubkey len 
  ^ OP_HASH160
    ^ Data element len
      ^ Data element
                                            ^ OP_EQUAL

where the zeros can be any byte.
How does that transform into an address starting with 3? You transform it the same way you would a normal Bitcoin address, but with a different version byte. Instead of 00, you would use 05. 
See also the P2SH address specification.

30017: <p>Im trying to understand how to obtain the p2SH address when decoding an output script.</p>

<pre><code>r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
</code></pre>

<p><a href="https://github.com/bitcoin/bitcoin/blob/0c8875d23b37bca9e37e0c387ab5365d4e8758c5/src/rpc/rawtransaction.cpp#L557" rel="nofollow">source</a></p>

<p>As far as I understand CScriptID is the hash160 of the script.  </p>
 2306: <p>So, a P2SH scriptPubKey looks like this:</p>

<pre><code>OP_HASH160 &lt;20 byte script hash&gt; OP_EQUAL
</code></pre>

<p>Serialized, in hex, that looks like:</p>

<pre><code>17a9140000000000000000000000000000000000000087

^ ScriptPubkey len 
  ^ OP_HASH160
    ^ Data element len
      ^ Data element
                                            ^ OP_EQUAL
</code></pre>

<p>where the zeros can be any byte.</p>

<p>How does that transform into an address starting with 3? You transform it the <a href="https://bitcoin.org/en/developer-reference#address-conversion" rel="nofollow">same way you would a normal Bitcoin address</a>, but with a different version byte. Instead of <code>00</code>, you would use <code>05</code>. </p>

<p>See also the <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="nofollow">P2SH address</a> specification.</p>
