# Need a function similar to python struct.unpack

**URL:** https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991
**Category:** General Usage
**Tags:** question
**Created:** [June 29, 2023, 7:18pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991 "2023-06-29T19:18:23Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jjgomezcadenas](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jjgomezcadenas](https://discourse.julialang.org/u/jjgomezcadenas)
#### Post date: [June 29, 2023, 7:18pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/1 "2023-06-29T19:18:23Z")

</div>

Hello everybody.  
I need to read binary file from a specific module (HydraHarp) which comes with python and matlab codes for decoding the file.

The python code uses extensively the struct.unapck functions. For example:

tyEmpty8 = struct.unpack(“\>i”, bytes.fromhex(“FFFF0008”))[0]

For reference, this will yield the value: -65528

This is equivalent to the MatLab code:

tyEmpty8 = hex2dec(‘FFFF0008’);

I need a similar conversion in Julia (I am using 1.9.1 in a MacOs with ARM processor) and I have been unable to reproduce the value using several approaches (including PyCall). Any help would be very welcoed!

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [June 29, 2023, 7:38pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/2 "2023-06-29T19:38:05Z")

</div>

I’m confused. Is the hex in a string or is it binary in a file. Here’s the literal function starting from a string.

```julia
julia> fromhex(hex) = first(reinterpret(Int32, [parse(UInt32, "0x"*hex)]))
fromhex (generic function with 1 method)

julia> fromhex("FFFF0008")
-65528

```

I got a feeling that this is not what you want.

You probably want something that is more like this.

```julia
julia> write("test.bin", 0xFFFF0008)
4

julia> read("test.bin", Int32)
-65528

```

In the above case, the file “test.bin” contains 4 bytes in little endian order.

```julia
julia> read("test.bin")
4-element Vector{UInt8}:
 0x08
 0x00
 0xff
 0xff

```

The above bytes are in little endian order. `08` comes first, and `FF` comes last. It seems like you need this to be read in big endian order.

```julia
julia> write("test.bin", hton(0xFFFF0008))
4

julia> read("test.bin")
4-element Vector{UInt8}:
 0xff
 0xff
 0x00
 0x08

julia> ntoh(read("test.bin", Int32))
-65528

```

Now `0xFF` comes first, and `0x08` comes last.

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [June 29, 2023, 7:38pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/3 "2023-06-29T19:38:42Z")

</div>

```julia
reinterpret(Int32, hex2bytes("FFFF0008")) .|> hton

```

Output this:

```julia
1-element Vector{Int32}:
 -65528

```

This only works for unpacking single datatype.

---

<div class="post-metadata">

### Author: ![jjgomezcadenas](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jjgomezcadenas](https://discourse.julialang.org/u/jjgomezcadenas)
#### Post date: [June 29, 2023, 7:51pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/4 "2023-06-29T19:51:23Z")

</div>

> [@mkitti](#):
>
> `julia> `

Thanks a lot for your explanations! Actually I had both type of problems, some data are “hard wire” in the code (e.g, tyEmpty8 = struct.unpack(“\>i”, bytes.fromhex(“FFFF0008”))[0]) and some data are read from the input file (e.g, tagInt = struct.unpack(“\<q”, inputfile.read(8))[0]), I think your explanations cover both cases. Again, thanks a lot

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 29, 2023, 8:33pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/5 "2023-06-29T20:33:01Z")

</div>

Another relevant post on [SO](https://stackoverflow.com/questions/64579524/unpack-binary-data-with-specific-format-in-julia). Looks very similar to @mkitti’s answer.

Also [this post](https://www.juliabloggers.com/learn-with-me-julia-structs-and-binary-i-o-3/) on Julia Bloggers.

---

<div class="post-metadata">

### Author: ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)
#### Post date: [June 29, 2023, 11:54pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/6 "2023-06-29T23:54:15Z")

</div>

Using [GitHub - analytech-solutions/CBinding.jl: Automatic C interfacing for Julia](https://github.com/analytech-solutions/CBinding.jl) should be very easy for such things as you can define the structure in C code directly.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 30, 2023, 5:40am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/7 "2023-06-30T05:40:12Z")

</div>

> [@mkitti](#):
>
> `julia> fromhex(hex) = first(reinterpret(Int32, [parse(UInt32, "0x"*hex)]))`

This seems a bit redundant, why not

```julia
fromhex(hex) = reinterpret(Int32, parse(UInt32, "0x"*hex))

```

?

---

<div class="post-metadata">

### Author: ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)
#### Post date: [June 30, 2023, 5:48am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/8 "2023-06-30T05:48:34Z")

</div>

You can avoid the string creation by manually specifying the base

```julia
fromhex(hex) = reinterpret(Int32, parse(UInt32, hex; base=16))

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 30, 2023, 5:59am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/9 "2023-06-30T05:59:39Z")

</div>

> [@jondea](#):
>
> `fromhex(hex) = reinterpret(Int32, parse(UInt32, hex; base=16))`

There’s also

```julia
fromhex(hex) = parse(UInt32, hex; base=16) % Int32

```

though I’m not 100% certain it’s exactly the same (on my phone here).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 30, 2023, 8:39am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/10 "2023-06-30T08:39:43Z")

</div>

> [@cchderrick](#):
>
> `reinterpret(Int32, hex2bytes("FFFF0008")) .|> hton`

```julia
julia> hex2bytes("FFFF0008")
4-element Vector{UInt8}:
 0xff
 0xff
 0x00
 0x08

julia> hex2bytes("FFFF008") # <- valid number
ERROR: ArgumentError: length of iterable must be even

```

> [@xgdgsc](#):
>
> Using [GitHub - analytech-solutions/CBinding.jl: Automatic C interfacing for Julia](https://github.com/analytech-solutions/CBinding.jl) should be very easy for such things as you can define the structure in C code directly.

What would be the advantage of doing this in C?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 2, 2023, 8:09am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/11 "2023-07-02T08:09:24Z")

</div>

I got curious enough about this that I started to implement the Python `struct` library in Julia.

```julia
(@v1.9) pkg> activate --temp
  Activating new project at `/tmp/jl_DFEY1E`

(jl_DFEY1E) pkg> add https://github.com/mkitti/PythonStructs.jl
    Updating git-repo `https://github.com/mkitti/PythonStructs.jl`
   Resolving package versions...
    Updating `/tmp/jl_DFEY1E/Project.toml`
  [ac2a11db] + PythonStructs v1.0.0-DEV `https://github.com/mkitti/PythonStructs.jl#main`
    Updating `/tmp/jl_DFEY1E/Manifest.toml`
  [ac2a11db] + PythonStructs v1.0.0-DEV `https://github.com/mkitti/PythonStructs.jl#main`

julia> using PythonStructs

julia> unpack(">i", hex2bytes("FFFF0008"))[begin]
-65528

julia> unpack("<i", hex2bytes("FFFF0008"))[begin]
134283263

julia> unpack(">I", hex2bytes("FFFF0008"))[begin]
0xffff0008

julia> hex2bytes("FFFF0008") |> unpack(">l") |> first
-65528

```

> **[GitHub - mkitti/PythonStructs.jl: An implementation of the Python struct...](https://github.com/mkitti/PythonStructs.jl)**
>
> An implementation of the Python struct standard library in Julia. - GitHub - mkitti/PythonStructs.jl: An implementation of the Python struct standard library in Julia.

Also see the [initial documentation](https://mkitti.github.io/PythonStructs.jl/dev/).

---

<div class="post-metadata">

### Author: ![jjgomezcadenas](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jjgomezcadenas](https://discourse.julialang.org/u/jjgomezcadenas)
#### Post date: [July 2, 2023, 8:53am UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/12 "2023-07-02T08:53:09Z")

</div>

Thanks a lot, this is really useful. The python library provides useful tools for binary data manipulation that are not obvious in Julia. I will go ahead and use your functions in my application

---

<div class="post-metadata">

### Author: ![jjgomezcadenas](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jjgomezcadenas](https://discourse.julialang.org/u/jjgomezcadenas)
#### Post date: [July 3, 2023, 3:15pm UTC](https://discourse.julialang.org/t/need-a-function-similar-to-python-struct-unpack/100991/13 "2023-07-03T15:15:23Z")

</div>

I have implemented the functions of your package in my code and they work just right. Thanks a lot.

juan jose gomez cadenas  
[jjgomezcadenas@gmail.com](mailto:jjgomezcadenas@gmail.com)
