Comparison of Android NFC Label Strings

I am having a problem with comparing two strings, one coming from a NFC tag, and another from an xml file. The code I use to write the tag looks like this:

private NdefMessage getTagAsNdef(Card new_card)
        boolean addAAR = false;
        String unique_id = new_card.getCardId();
        byte[] bytes = unique_id.getBytes(Charset.forName("UTF-8"));
        byte[] payload = new byte[bytes.length + 1];
        NdefRecord rtdUriRecord = new NdefRecord(
        NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
            NdefRecord new_record = null;
                new_record = createRecord(new_card);
            } catch (UnsupportedEncodingException e)
            return new NdefMessage(new NdefRecord[] {rtdUriRecord, new_record});
        } else
            return new NdefMessage(new NdefRecord[]

I tried to remove all the language and encoding info usually set in the payload following the advice found here: Comparing Data Read from NFC Tag The comparison I do here after reading the tag however always fails, although the string values printed in the log are identical:

looking at this_card_id -6465415649291849135
      compare to tag_id -6465415649291849135

I got the length of strings to show the problem:

looking at this_card_id 20
      compare to tag_id 21

So the tag id has a hidden character which I have been unable to get rid of. Here is the code for the comparison:

private void associateWithCardsFile(String tag_id)
    String method = "associateWithCardsFile";
    Log.i(DEBUG_TAG, method+" tag_id "+tag_id);
    boolean found = false;
    Enumeration e = cards.keys();
    Log.i(DEBUG_TAG, method+" cards "+cards.size());
    while (e.hasMoreElements())
        String this_card_id = (String)e.nextElement();
        Log.i(DEBUG_TAG, method+" looking at this_card_id "+this_card_id.length());
        Log.i(DEBUG_TAG, method+"       compare to tag_id "+tag_id.length());
        String card_id_str = UtilityTo.encodeThisString(this_card_id, "UTF-8");
        String tag_id_str = UtilityTo.encodeThisString(tag_id, "UTF-8");
        if (card_id_str.equals(tag_id_str))
            Card matching_card = cards.get(this_card_id);
            String word = UtilityTo.getWord(matching_card);
            Toast.makeText(this, word, Toast.LENGTH_LONG ).show();
            Log.i(DEBUG_TAG, method+" 1 match"+matching_card.getText()+" "+matching_card.getDefinition()+" "+matching_card.getWordType());
            found = true;
    if (!found)
        Toast.makeText(this, "Not a game card!", Toast.LENGTH_LONG ).show();
        Log.i(DEBUG_TAG, method+" card not found");

In the UtilityTo class, I have this method which works well for working with foreign languages.

public static String encodeThisString(String original_value, String encoding)
        byte[] utf8Bytes = original_value.getBytes(encoding);
        String new_value = new String(utf8Bytes, encoding);
        return new_value;
    } catch (UnsupportedEncodingException e)
        return null;
    } catch (java.lang.NullPointerException n)
        return null;

I also tried some advice from StackOverflow like this: tag_id_str = new String(tag_id.getBytes(“UTF-8”), “ascii”) to no avail.

Can someone please explain how to either write the tag so that there are no hidden characters and basic string comparisons will work, or how to modify the string from the tag so it will equate to a string with the same number. Thanks.

p.s. Here is the write method from the Android Developer NFC Basics that I started using after writing this. It causes the same problem as noted above: the tag_id is 1 character longer than card_id from the xml file.

public NdefRecord createRecord(String payload)
    String language = "en";
    boolean encodeInUtf8 = true;
    byte[] langBytes = language.getBytes(Charset.forName("US-ASCII"));
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    byte[] textBytes = payload.getBytes(utfEncoding);
    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    char status = (char) (utfBit + langBytes.length);
    byte[] data = new byte[1 + langBytes.length + textBytes.length];
    data[0] = (byte) status;
    System.arraycopy(langBytes, 0, data, 1, langBytes.length);
    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
    NdefRecord.RTD_TEXT, new byte[0], data);
    return record;
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Comparison of Android NFC Label Strings

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录