pxb1988/dex2jar · error · IllegalArgumentException

bad utf-8 byte %02x at offset %08x

Error message

bad utf-8 byte %02x at offset %08x

What it means

Thrown by Utf8Utils.throwBadUtf8 (from utf8BytesToString) when a byte at a given offset is not a valid MUTF-8 lead or continuation byte. IllegalArgumentException naming the bad byte value in hex and its offset in the byte array.

Solutions

  1. Validate/repair the input bytes before conversion, or decode defensively with a try-catch for IllegalArgumentException
  2. Check the byte offset passed to utf8BytesToString points at the actual string start
  3. Re-obtain or rebuild the DEX file if corruption is confirmed

Example fix

// before
String s = Utf8Utils.utf8BytesToString(bytes, offset, length);
// after
String s;
try {
    s = Utf8Utils.utf8BytesToString(bytes, offset, length);
} catch (IllegalArgumentException e) {
    s = new String(bytes, offset, length, StandardCharsets.ISO_8859_1); // lossy fallback
}
Defensive patterns

Strategy: try-catch

Validate before calling

try { Utf8Utils.utf8BytesToString(bytes, off, len); } catch (IllegalArgumentException e) { /* invalid */ }

Type guard

null

Try / catch

try { s = Utf8Utils.utf8BytesToString(bytes, off, len); } catch (IllegalArgumentException e) { s = lossyDecode(bytes, off, len); }

Prevention

When it happens

Trigger: Calling Utf8Utils.utf8BytesToString on bytes containing invalid MUTF-8 sequences — bad lead bytes (>= 0xF0 or 0x80-0xBF as lead) or non-continuation trailing bytes.

Common situations: Reading strings from corrupted DEX files, decoding raw UTF-16 or Latin-1 bytes as MUTF-8, wrong offsets into string data.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of pxb1988/dex2jar@b5bda4fb49 (2026-09-08). Data as JSON: /api/errors/1022d7c1afadcc43. Report an issue: GitHub.

Appendix: source

Thrown at dex-reader/src/main/java/com/googlecode/d2j/util/Utf8Utils.java:185

            outAt++;
        }

        return new String(chars, 0, outAt);
    }

    /**
     * Helper for {@link #utf8BytesToString}, which throws the right exception for a bogus utf-8 byte.
     * 
     * @param value
     *            the byte value
     * @param offset
     *            the file offset
     * @return never
     * @throws IllegalArgumentException
     *             always thrown
     */
    private static String throwBadUtf8(int value, int offset) {
        throw new IllegalArgumentException("bad utf-8 byte " + String.format("%02x", value) + " at offset "
                + String.format("%08x", offset));
    }

    public static void writeEscapedChar(Writer writer, char c) throws IOException {
        if ((c >= ' ') && (c < 0x7f)) {
            if ((c == '\'') || (c == '\"') || (c == '\\')) {
                writer.write('\\');
            }
            writer.write(c);
            return;
        } else if (c <= 0x7f) {
            switch (c) {
            case '\n':
                writer.write("\\n");
                return;
            case '\r':
                writer.write("\\r");
                return;

View on GitHub (pinned to b5bda4fb49)