loadBytes() not reading the entirety of JPEG file?

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.

  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:

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:

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

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:

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();
    }
  }

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:

:)

1 Like

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!