# loadBytes() not reading the entirety of JPEG file?

**URL:** https://discourse.processing.org/t/loadbytes-not-reading-the-entirety-of-jpeg-file/44241
**Category:** Coding Questions
**Created:** [April 7, 2024, 5:14pm UTC](https://discourse.processing.org/t/loadbytes-not-reading-the-entirety-of-jpeg-file/44241 "2024-04-07T17:14:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bbcwarrior](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bbcwarrior](https://discourse.processing.org/u/bbcwarrior)
#### Post date: [April 7, 2024, 5:14pm UTC](https://discourse.processing.org/t/loadbytes-not-reading-the-entirety-of-jpeg-file/44241/1 "2024-04-07T17:14:50Z")

</div>

I’m trying to create something like a hex editor for JPEG files in processing, and running this piece of code with any JPEG file results in a correct output, but one that excludes major parts of the file.

```auto
  byte b[] = loadBytes("cat.jpg"); 
 
  for (int i = 0; i < b.length; i++) { 
    if ((i % 10) == 0) { 
      println(); 
    } 
    int a = b[i] & 0xff; 
    print(hex(a,2) + " "); 
  } 
  println(); 
  

```

This piece of code will output something like:

```auto
B2 92 E4 34 6D 83 09 52 0F 6E 
FB 7F 97 D4 20 CF A6 5E 44 D3 
...

```

However, when using a separate hex editor, one will find that the program has excluded the majority of the file, being that the output, when searched in the editor only appears later in the file:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/f/af8f7ad5031333e8c788982c8714ce130177631b.png)

Can anyone think of why this may be occurring or any potential solutions? Thanks!

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 7, 2024, 8:20pm UTC](https://discourse.processing.org/t/loadbytes-not-reading-the-entirety-of-jpeg-file/44241/2 "2024-04-07T20:20:47Z")

</div>

Hello @bbcwarrior,

I see no difference between the simple hex viewer and the contents of a JPG (or any other file) with a hex editor.

I tweaked your file a bit to show the 0x10 (16) lines per row:

```auto
byte b[] = loadBytes("Capture.JPG");

println("File size (bytes):", b.length);

for (int i = 0; i < b.length; i++) 
  {
  if ((i % 16) == 0 || i == 0) 
    {
    print( hex(i, 4) + " ");
    }

  int a = b[i] & 0xff;
  print(hex(a, 2) + " ");

  if (((i+1) % 16) == 0) 
    {
    println();
    }
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/5/05f3051fe2700330d766035e0fe0806eccd27675.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/c/4c4797e141a4da97b7cb3a5f0f1c8910dfec1c22.png)

You may not be seeing everything in your console.

If you want to create an image editor then a hex editor will not work for a JPEG.  
I suggest you do a bit of research on this point.

Resources:

> **[Images and Pixels](https://processing.org/tutorials/pixels/)**
>
> How to load and display images as well as access their pixels.

`:)`

---

<div class="post-metadata">

### Author: ![bbcwarrior](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bbcwarrior](https://discourse.processing.org/u/bbcwarrior)
#### Post date: [April 7, 2024, 8:43pm UTC](https://discourse.processing.org/t/loadbytes-not-reading-the-entirety-of-jpeg-file/44241/3 "2024-04-07T20:43:13Z")

</div>

Hi glv,

Thanks for the reply, looks like my console was cutting off the top half of my output, but it was thanks to you I was able to figure that out,

Thanks!
