<blockquote>
<p><strong>If someone could explain the pack, unpack functions</strong></p>
</blockquote>
<p>The PHP manual covers <a href="https://www.php.net/manual/en/function.pack.php" rel="nofollow noreferrer">pack</a> and <a href="https://www.php.net/manual/en/function.unpack.php" rel="nofollow noreferrer">unpack</a></p>
<blockquote>
<p>Pack given arguments into a binary string according to format.</p>
</blockquote>
<blockquote>
<p>Unpacks from a binary string into an array according to the given format.</p>
</blockquote>
<p>So I would expect the code to produce a character array. This might be useful in PHP but not useful in Python.</p>
<p>The format characters are explained under pack</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>NUL-padded string</td>
</tr>
<tr>
<td>A</td>
<td>SPACE-padded string</td>
</tr>
<tr>
<td>h</td>
<td>Hex string, low nibble first</td>
</tr>
<tr>
<td>H</td>
<td>Hex string, high nibble first</td>
</tr>
<tr>
<td>c</td>
<td>signed char</td>
</tr>
<tr>
<td>C</td>
<td>unsigned char</td>
</tr>
<tr>
<td>s</td>
<td>signed short (always 16 bit, machine byte order)</td>
</tr>
<tr>
<td>S</td>
<td>unsigned short (always 16 bit, machine byte order)</td>
</tr>
</tbody>
</table>
</div>
<p>etc</p>
<p>However I would avoid performing a statement by statement or function by function translation and try to understand what the whole section of code is trying to achieve and then work out how best to accomplish that using the standard libraries available in Python. I find libraries and idioms vary greatly from language to language and an overly literal translation can end up being fragile, inefficient and incomprehensible. Your experience may differ though.</p>
<p>Note that you are asking about PHP and Python rather than about Bitcoin specifics. This question might do better on stackoverflow.com.</p>

CC BY-SA 4.0
2022-11-18T11:21:18.733Z
2022-11-18T11:41:22.690Z
2022-11-18T11:41:22.690Z
RedGrittyBrick
<p>These pack/unpack php functions seem to be exactly as the identically named perl functions.</p>
<p>So <code>pack(&quot;H*&quot;, ...)</code>  converts an ascii string to an byte-string.</p>
<p>And <code>unpack(&quot;C*&quot;, ...)</code>  converts a byte-string to an array of byte values.</p>
<p>In python, <code>pack(&quot;H*&quot;, $scriptpubkey)</code>  would be:  <code>binascii.a2b_hex(scriptpubkey)</code></p>
<p>The second step is not needed in python, since byte-arrays are already lists of byte values in python. If you access a single element: <code>a[0]</code> you would get the integer value  <code>0x41</code>  in this case. While accessing a subrange: <code>a[1:]</code> would get you a byte-array containing the pubkey.</p>
<p>Then another remark, if your variable contains a hexadecimal representation of your script, it might be clearer to indicate that in the variablename, for instance by naming it <code>hex_scriptpubkey</code>.</p>

CC BY-SA 4.0
2022-11-18T17:30:22.963Z
2022-11-18T17:30:22.963Z
Willem Hengeveld
<p>I'm trying to convert this php code:</p>
<pre><code>$scriptpubkey = '4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac'
$values = unpack(&quot;C*&quot;, pack(&quot;H*&quot;, $scriptpubkey));
</code></pre>
<p>to python. So far, i have done the following:</p>
<pre><code>PHP: pack(&quot;H*&quot;, $scriptpubkey) -&gt; Python: binascii.unhexlify(scriptpubkey)
</code></pre>
<p>I am not sure if it's correct though. If someone could explain the pack, unpack functions and what the characters &quot;C*&quot;, &quot;H*&quot; do, it would be much appreciated.</p>

<bitcoin-core><python><php>
What is the python equivalent of this php code $values = unpack("C*", pack("H*", $scriptpubkey));
Andreas
# What is the python equivalent of this php code $values = unpack("C*", pack("H*", $scriptpubkey));

(Asked by: username_0 on 2022-11-18)

I'm trying to convert this php code:

```
$scriptpubkey = '4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac'
$values = unpack("C*", pack("H*", $scriptpubkey));

```

to python. So far, i have done the following:

```
PHP: pack("H*", $scriptpubkey) -> Python: binascii.unhexlify(scriptpubkey)

```

I am not sure if it's correct though. If someone could explain the pack, unpack functions and what the characters "C\*", "H\*" do, it would be much appreciated.

**Tags:** bitcoin-core, python, php


---

## 2 answers

### Answer by username_1 on 2022-11-18. Score: 0

> **If someone could explain the pack, unpack functions**

The PHP manual covers [pack](https://www.php.net/manual/en/function.pack.php) and [unpack](https://www.php.net/manual/en/function.unpack.php)

> Pack given arguments into a binary string according to format.

> Unpacks from a binary string into an array according to the given format.

So I would expect the code to produce a character array. This might be useful in PHP but not useful in Python.

The format characters are explained under pack

| Code | Description |
| --- | --- |
| a | NUL-padded string |
| A | SPACE-padded string |
| h | Hex string, low nibble first |
| H | Hex string, high nibble first |
| c | signed char |
| C | unsigned char |
| s | signed short (always 16 bit, machine byte order) |
| S | unsigned short (always 16 bit, machine byte order) |

etc

However I would avoid performing a statement by statement or function by function translation and try to understand what the whole section of code is trying to achieve and then work out how best to accomplish that using the standard libraries available in Python. I find libraries and idioms vary greatly from language to language and an overly literal translation can end up being fragile, inefficient and incomprehensible. Your experience may differ though.

Note that you are asking about PHP and Python rather than about Bitcoin specifics. This question might do better on stackoverflow.com.


---

### Answer by username_2 on 2022-11-18. Score: 0

These pack/unpack php functions seem to be exactly as the identically named perl functions.

So `pack("H*", ...)` converts an ascii string to an byte-string.

And `unpack("C*", ...)` converts a byte-string to an array of byte values.

In python, `pack("H*", $scriptpubkey)` would be: `binascii.a2b_hex(scriptpubkey)`

The second step is not needed in python, since byte-arrays are already lists of byte values in python. If you access a single element: `a[0]` you would get the integer value `0x41` in this case. While accessing a subrange: `a[1:]` would get you a byte-array containing the pubkey.

Then another remark, if your variable contains a hexadecimal representation of your script, it might be clearer to indicate that in the variablename, for instance by naming it `hex_scriptpubkey`.


---

