Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A Java service parses a binary file produced by a C program on x86 and all numbers come out huge and wrong, but small byte values are fine. After switching to little-endian the integers are right, yet 64-bit timestamps at offset 6 are still wrong. What do you check?

Integers that became correct after switching to little-endian tell us the producer writes little-endian, so byte order is solved. A 64-bit field that is still wrong at offset 6 almost always means struct padding: the C compiler aligned the 8-byte timestamp to offset 8, so the Java code is reading two padding bytes plus six bytes of the real value. We confirm it from the C struct definition and a hex dump, and also check type sizes, signedness and the timestamp's unit.

Why this question is asked

Reading binary data written by C code is a classic integration task: device logs, trading feeds, legacy batch files. The first bug, byte order, is well known. The second bug, a field that is wrong even though the byte order is right, is where people get stuck, because the cause is invisible in the Java code. Interviewers ask this as a debugging question to see whether we reason from evidence step by step. In this answer, we will read what each symptom tells us, recap how C lays out structs, reproduce the wrong timestamp with real numbers, fix the parser, run through the other checks (type sizes, signedness, units), and finish with the long-term fix.

1Reading the Symptoms

Each symptom narrows the search. Numbers that were huge while small byte values were fine is the signature of a byte-order mismatch: single bytes have no order, but every multi-byte value was being read backwards. Switching the ByteBuffer to ByteOrder.LITTLE_ENDIAN fixed the integers, which confirms that the producer, an x86 program, wrote its values in the processor's native little-endian order.

Now the timestamps at offset 6 are still wrong while the integers are right. Byte order applies to every field equally, so it cannot explain one field being wrong. Something about that field's position or type must differ from what the Java code assumes. The offset 6 is the clue: it is not a multiple of 8.

2A Quick Recap of Struct Padding and Alignment

Processors read memory fastest, and on some architectures only correctly, when a value starts at an address that is a multiple of its size: a 4-byte int at a multiple of 4, an 8-byte int64 at a multiple of 8. This is called alignment. To achieve it, C compilers insert unused padding bytes between struct fields. The programmer never sees them in the source code, but they are written to the file when the program writes the struct with fwrite.

Java
// The producer's definition (C)
struct record {
uint32_t id; // offset 0, 4 bytes
uint16_t flags; // offset 4, 2 bytes
// offset 6, 2 bytes of padding added by the compiler
int64_t timestamp; // offset 8, 8 bytes (aligned to 8)
}; // sizeof(struct record) == 16, not 14
Bytes of one record (little-endian)
OffsetBytesMeaning
0-307 00 00 00id = 7
4-503 00flags = 3
6-700 00padding inserted by the compiler
8-1500 3b c2 f5 92 01 00 00timestamp = 1730700000000 (ms)

A reader who adds up the field sizes, 4 plus 2, expects the timestamp at offset 6 and the record size to be 14. Both are wrong: the timestamp starts at 8 and every record is 16 bytes. If the Java code also steps through the file 14 bytes at a time, every record after the first is misaligned too, and the corruption gets worse as it goes.

3Reproducing the Wrong Timestamp

We can check the theory with the actual numbers. Reading eight bytes from offset 6 takes the two padding bytes as the lowest bytes of the value and drops the real top two bytes.

Java
ByteBuffer rec = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
int id = rec.getInt(0); // 7
short flags = rec.getShort(4); // 3
long wrong = rec.getLong(6); // 113423155200000000: the real value shifted by 16 bits
long ts = rec.getLong(8); // 1730700000000 -> 2024-11-04T06:00:00Z

Notice that the wrong value is the right value multiplied by 65,536, because the real bytes moved up two places. When the padding bytes contain leftover garbage rather than zeros, the error is noisier, but the pattern of "wrong by a shift" is a strong sign of an offset problem rather than a byte-order problem. The fix is to read at offset 8 and advance 16 bytes per record. If the producer used a packing directive such as #pragma pack(1) or __attribute__((packed)), there is no padding, and offset 6 would be right, which is exactly why we confirm from the producer's source instead of guessing.

4The Other Things to Check

Type sizes differ between platforms. A C long is 4 bytes on 64-bit Windows and 8 bytes on 64-bit Linux and macOS, and int and enum sizes are compiler choices. Fixed-width types such as uint32_t and int64_t remove the doubt, so if the struct uses long or int we ask which compiler and platform built the producer. Asking the C team to print sizeof(struct record) and offsetof for each field settles it in one minute.

Signedness is the next trap. Java has no unsigned integer types, so a uint32_t of 3,000,000,000 reads as a negative int. We widen with Integer.toUnsignedLong, Short.toUnsignedInt or Byte.toUnsignedInt, and use Long.toUnsignedString or compareUnsigned for unsigned 64-bit values.

Finally the unit and epoch of the timestamp. Even at the right offset, the value might be seconds, milliseconds or microseconds since 1970, or a Windows FILETIME counting 100-nanosecond intervals since 1601. A known record, such as one written at a time we know, tells us which one.

Java
long count = Integer.toUnsignedLong(rec.getInt(0)); // 3000000000 instead of -1294967296
int flags = Short.toUnsignedInt(rec.getShort(4)); // 65535 instead of -1
Instant when = Instant.ofEpochMilli(rec.getLong(8)); // only if the unit really is milliseconds

5The Long-Term Fix

The parser now works, but the format still lives only in a C header that Java developers guessed at. The lasting fix is to define the format explicitly: a short specification that lists every field's offset, size, signedness and byte order, padding included, a version or magic number at the start of the file, and sample files with their expected decoded values that both teams run in their tests. Better still, for new data, a schema format such as Protocol Buffers removes hand-written offsets altogether, because both sides generate their code from one definition.

Step throughDebugging a binary parse, field by field
  1. Are multi-byte values huge while bytes are fine?
    Yesbyte order: set LITTLE_ENDIANNonext check
  2. Is one field wrong at a non-aligned offset?
    Yespadding: get sizeof and offsetof from the producerNonext check
  3. Large values negative?
    Yesunsigned: widen with toUnsignedLongNonext check
  4. Value plausible but dates odd?
    Yesunit or epoch: compare with a known recordNowrite the spec and shared test files

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

How can Java code check the layout without the C source?

With a hex dump of a record whose values we know, for example one the producer team writes on purpose with id 1, flags 2 and a fixed timestamp. Finding where those byte patterns appear gives the real offsets, padding and record size.

Could the Foreign Function and Memory API help here?

Yes. A MemoryLayout.structLayout can describe the record with explicit padding layouts and little-endian value layouts, and generates VarHandles for each field. The layout documents the format in code and computes offsets for us, which avoids hand-typed numbers like 6 and 8.

Why did the integers before the timestamp still decode correctly?

Because the padding comes after them. Fields before the first gap sit at the offsets we expect; only fields after it are shifted, which is why the error appears at one specific field.

7Conclusion

In this answer, we used each symptom to narrow the cause: huge numbers with correct bytes meant byte order, fixed with LITTLE_ENDIAN, and one 64-bit field still wrong at offset 6 pointed to padding, because the C compiler aligned it to offset 8 and made the record 16 bytes. We confirmed it with real numbers, then checked type sizes such as long, signedness with the toUnsigned methods, and the timestamp's unit, and ended with an explicit, shared format definition as the lasting fix.

What to say in the interviewAbout 90 seconds aloud

Open with

The first symptom, huge integers but correct single bytes, is classic byte order, and since switching to LITTLE_ENDIAN fixed the integers I know the x86 producer writes little-endian.

Then, if they want more

The timestamps at offset 6 being wrong is a different problem, because byte order affects every field equally.

My first suspect is struct padding: a C compiler aligns an 8-byte int64 to an offset that is a multiple of 8, so after a 4-byte id and a 2-byte flags field it inserts two padding bytes and the timestamp really starts at offset 8, and the record is 16 bytes, not 14.

Reading at 6 gives the value shifted by two bytes, which is exactly the kind of wrong you see. I would confirm it rather than guess: ask for the struct definition and any pragma pack, or have the C side print sizeof and offsetof, and compare with a hex dump of a known record.

While I am there I check type sizes, since a C long is 4 bytes on Windows and 8 on Linux, signedness, because Java has no unsigned types and I need Integer.toUnsignedLong for uint32 values, and the timestamp unit and epoch.

Long term, I would write the format down explicitly with offsets, sizes and padding, add a version field and shared test files, or move new data to a schema format like Protocol Buffers.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Java service parses a binary file produced by a C program on x86 and all numbers come...": https://scrivio-interview-library.netlify.app/java/io-and-nio/8d51e24cf2/