# Reading 12 bit integers from file

**URL:** <https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173>\
**Category:** General Usage\
**Tags:** question, binaryio, matlab, filesystem\
**Created:** [April 28, 2021, 3:01pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173 "2021-04-28T15:01:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawly](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Shawly](https://discourse.julialang.org/u/Shawly)\
**Post date:** [April 28, 2021, 3:01pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173/1 "2021-04-28T15:01:21Z")

</div>

I am new to Julia and trying to port some code from Matlab. I have an image file saved as a raw binary file with 12 bit precision. In Matlab I can use fread ([Read data from binary file - MATLAB fread - MathWorks United Kingdom](https://uk.mathworks.com/help/matlab/ref/fread.html)):

A = fread(fileID, sizeA, precision) with precision equal to 12.

In julia, I can only find methods that support a precision which is a multiple of 8. Are there any methods available?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 28, 2021, 3:20pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173/2 "2021-04-28T15:20:15Z")

</div>

I guess you should read 24 bits at a time and split them up.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 28, 2021, 3:22pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173/3 "2021-04-28T15:22:50Z")

</div>

Welcome to our community 🎊

uhhh, in the documentation link you gave `12` is not a valid precision, there is `ubit12` and `bit12` what one did you use? Did you have negative numbers?

---

<div class="post-metadata">

**Author:** ![Shawly](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Shawly](https://discourse.julialang.org/u/Shawly)\
**Post date:** [April 28, 2021, 3:27pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173/4 "2021-04-28T15:27:31Z")

</div>

Thanks! I figured this might be the way to go but I was hoping to avoid it.

---

<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:** [April 28, 2021, 3:42pm UTC](https://discourse.julialang.org/t/reading-12-bit-integers-from-file/60173/5 "2021-04-28T15:42:20Z")

</div>

You can directly read 12 bit numbers in Matlab, but my experience has been that it’s _very_ slow. Reading as ordinary bytes and splitting manually was dramatically faster.
