I'm using [`pack / unpack`](http://perldoc.perl.org/functions/pack.html) in perl to convert hex values to decimal like this:

```
print unpack "S", reverse pack "H4", "1230";
> 4656

```

Everything is good while data is 4 hex symbols.
If data length is not enough to fit requested format pack adds zeroes to the end. 

Like this:

```
print unpack "b*", pack "H4", "FF";
> 1111111100000000

```

It causes:

```
print unpack "S", reverse pack "H4", "123";
> 4656

```

While I'd like it to be the same as [`oct`](http://perldoc.perl.org/functions/oct.html):

```
print oct("0x123");
> 291

print unpack "S", reverse pack "H4", "0123";
> 291

```

**How to make pack to add zeroes at the beginning of the data when data length not enough to fit format?**

Short FAQ
---------

**What are you trying to do?**
I have bunch of functions to deal with different types of data (`float`, `short`, `long`, `ushort`, `ulong`, etc). All conversions are done in using `pack`/`unpack`. These functions are needed to convert data produced by third-party.

**Why not to use `oct` or `hex`?**
Because I have to deal with signed integer and float. And I want all functions to be similar for better code understanding.

**Could you provide more examples?** Here they are:

**Example 1:** Signed short integer (full data received). System receives hex data `018F` from third party. This data supposed to be `signed short`, so it is processed in the following way:

```
print unpack "s", reverse pack "H4", "018F";
> 399

```

**Example 2:** Signed short integer (half data received). System receives hex data `8F` from third party. `8F` is only 1 byte, while 2 bytes needed to form short integer. It is processed in the same way:

```
print unpack "s", reverse pack "H4", "8F";
> -28928

```

But third party means `008F` not `8F00`:

```
print unpack "s", reverse pack "H4", "008F";
> 143

print unpack "s", reverse pack "H4", "8F00";
> -28928

```

**What about endianness?**
In all examples received data is `big-endian`, while my system is `little-endian` that is why `reverse` used.

**How do you deal with it now?** I'm using sprintf to add zeroes.

```
print unpack "s", reverse pack "H4", sprintf "%04s", "8F";
> 143

```

**What version of perl do you use?** 5.8.8