0

I'm working on an Android app using zbar to decode QR. The structure of the QR is: "SpecificPrefix/[Number]".

String d = sym.getData(); // I get the QR text from the symbol
if(d.startsWith(QRUri)) { // QRUri is a string const containig the prefix
    int id;
    try {
        Log.d("NOT", "QR Subst \"" + d.substring(QRUri.length()) + "\""); // Print the data after the Prefix
        id = Integer.parseInt(d.substring(QRUri.length())); // Try to parse it to int
    } catch(NumberFormatException e) {
        e.printStackTrace();
        id = 0;
    }
}

This code worked for every QR so far, but today I found that for "SpecificPrefix/76" it didn't.

This is the log:

D/NOT﹕ QR Subst "76"
D/NOT﹕ No Int found!
W/System.err﹕ java.lang.NumberFormatException: Invalid int: "76"
W/System.err﹕ at java.lang.Integer.invalidInt(Integer.java:138)
W/System.err﹕ at java.lang.Integer.parse(Integer.java:375)
W/System.err﹕ at java.lang.Integer.parseInt(Integer.java:366)
W/System.err﹕ at java.lang.Integer.parseInt(Integer.java:332)
W/System.err﹕ at [...].MainActivity$3.onPreviewFrame(MainActivity.java:146)
W/System.err﹕ at android.hardware.Camera$EventHandler.handleMessage(Camera.java:841)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4921)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

java.lang.NumberFormatException: Invalid int: "76" I can't get why it doesn't work!

Tested on Android 4.1.2 but it seems not to work on 4.4.2 either.

Thanks in advance to anyone hoping to work it out.

DarioDF
  • 301
  • 1
  • 10
  • at a guess, I would bet you have invisible characters in your string. Check this post out, and maybe try it on that specific string. http://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java – DrewJordan Mar 13 '15 at 19:17
  • Did you try to trim the with `id = Integer.parseInt(d.substring(QRUri.length()).trim());`? – MrEngineer13 Mar 13 '15 at 19:18

1 Answers1

1

After copying the entire string into Eclipse to test this, I can duplicate this issue. I opened the source code in "binary mode" in TextPad to see if there were some strange characters in the string, and sure enough, there are.

22 EF BB BF 37 36 22     "???76"

I googled the string "EFBBBF" and found out that sequence of bytes is the Unicode "byte order mark".

When this BOM character, U+FEFF, is serialized in UTF-8 encoding, it becomes an octet sequence of EF BB BF (\xEFBBBF).

Sure enough, I hit delete when my cursor was in between the first double-quote and the 7, and no characters disappeared. (A second delete removed the double-quote, so I put it back.) After that, Integer.parseInt was able to find the integer 76.

You'll need to adjust your program to account for this character if it's appearing in your strings.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Thank you, the first thing I tried was to trim the string, but those chars are not whitespaces. The error was on the QR: I built the QR with a py script from a CSV file and the first row was the one with 76! – DarioDF Mar 13 '15 at 20:00