String Android NFC标记字符串比较

String Android NFC标记字符串比较,string,tags,comparison,nfc,String,Tags,Comparison,Nfc,我在比较两个字符串时遇到问题,一个来自NFC标记,另一个来自xml文件。 我用于编写标记的代码如下所示: private NdefMessage getTagAsNdef(Card new_card) { boolean addAAR = false; String unique_id = new_card.getCardId(); byte[] bytes = unique_id.getBytes(Charset.f

我在比较两个字符串时遇到问题,一个来自NFC标记,另一个来自xml文件。 我用于编写标记的代码如下所示:

   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);
        if(addAAR) 
        {
            NdefRecord new_record = null;
            try 
            {
                new_record = createRecord(new_card);
            } catch (UnsupportedEncodingException e) 
            {
                e.printStackTrace();
            }
            return new NdefMessage(new NdefRecord[] {rtdUriRecord, new_record}); 
        } else 
        {
            return new NdefMessage(new NdefRecord[] 
        {
                rtdUriRecord});
        }
    }
我尝试删除负载中通常设置的所有语言和编码信息,如下所示: 尽管日志中打印的字符串值相同,但我在读取标记后进行的比较始终失败:

looking at this_card_id -6465415649291849135
      compare to tag_id -6465415649291849135
我得到了字符串的长度以显示问题:

looking at this_card_id 20
      compare to tag_id 21
所以标签id有一个隐藏的字符,我一直无法摆脱。 以下是用于比较的代码:

private void associateWithCardsFile(String tag_id)
{
    String method = "associateWithCardsFile";
    Log.i(DEBUG_TAG, method+" tag_id "+tag_id);
    tag_id.trim();
    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();
        this_card_id.trim();
        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());
            turn_cards.add(matching_card);
            found = true;
            showCards();
            break;
        }
    }
    if (!found)
    {
        Toast.makeText(this, "Not a game card!", Toast.LENGTH_LONG ).show();
        Log.i(DEBUG_TAG, method+" card not found");
    }
}
在UtilityTo类中,我有一个方法,它适用于外语

public static String encodeThisString(String original_value, String encoding)
{
    try
    {
        byte[] utf8Bytes = original_value.getBytes(encoding);
        String new_value = new String(utf8Bytes, encoding);
        return new_value;
    } catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
        return null;
    } catch (java.lang.NullPointerException n)
    {
        n.printStackTrace();
        return null;
    }
}
我还尝试了StackOverflow的一些建议,如:tag\u id\u str=new String(tag\u id.getBytes(“UTF-8”),“ascii”),但没有效果。
有人能解释一下如何写标签,这样就不会有隐藏字符,基本的字符串比较也会起作用,或者如何修改标签中的字符串,使其等同于具有相同数字的字符串。 谢谢

p、 美国。 这是我在写这篇文章之后开始使用的Android开发者NFC Basics的write方法。它会导致与上面提到的问题相同的问题:tag_id比xml文件中的card_id长1个字符

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;
}
private String removeHiddenCharacters(String message)
{
  String dash = "-";
  char d = dash.charAt(0);
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < message.length(); i++)
  {
    char c = message.charAt(i);
    if (Character.isDigit(c))
    {
        sb.append(c);
    } else if (c == d)
    {
        sb.append(d);
    }
  }
  String result = new String(sb);
  Log.i(DEBUG_TAG, " result "+result);
  return result;
}
public NdefRecord createRecord(字符串有效负载)
{
字符串语言=“en”;
布尔编码inutf8=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试试这个,看看这是否解决了您的问题。可能您的编码/解码文本记录不正确。

因为我只在每个标记上存储一个长字符,所以我制定了一种方法,从标记消息中排除所有其他字符。 现在,我可以将标记id字符串与xml文件中的id字符串进行比较

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;
}
private String removeHiddenCharacters(String message)
{
  String dash = "-";
  char d = dash.charAt(0);
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < message.length(); i++)
  {
    char c = message.charAt(i);
    if (Character.isDigit(c))
    {
        sb.append(c);
    } else if (c == d)
    {
        sb.append(d);
    }
  }
  String result = new String(sb);
  Log.i(DEBUG_TAG, " result "+result);
  return result;
}
private String removeHiddenCharacters(字符串消息)
{
字符串破折号=“-”;
字符d=破折号字符(0);
StringBuffer sb=新的StringBuffer();
对于(int i=0;i感谢托马斯的帮助。由于我的需求很简单,NFC,我几乎完成了读写方法,我终于能够解决这个问题。NDF工具看起来是个不错的选择。如果我从头开始,我可能会节省一些时间。ord?它的有效负载是一个(任意)字节数组,因此问题源会更少。