可能重复:
Android NDEF记录有效负载上的奇怪字符
我试图从NFC标签中读取一些纯文本.我的代码在下面;
public void processReadIntent(Intent intent){
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
Log.d("msg", msg.getRecords()[0].getPayload().toString());
String PatientId=new String(msg.getRecords()[0].getPayload());
String UserName="nurse";
String Password="nurse";
Toast.makeText(getApplicationContext(), PatientId, Toast.LENGTH_LONG).show();
//tv.setText(new String(msg.getRecords()[0].getPayload()));
}
Run Code Online (Sandbox Code Playgroud)
但是,这里的问题是当我读取数据时,我可以看到我想要的数据在开始时有一个'en'. 例如:如果我在'john'中的实际数据,当我阅读时,我可以将其视为'enjohn'. 我知道'en'是语言标题.但是我该如何删除它?
我尝试过子串,但之后甚至没有工作......
有关如何删除此语言标题的任何想法???
从第二个链接获取的片段.
try
{
byte[] payload = record.getPayload();
/*
* payload[0] contains the "Status Byte Encodings" field, per the
* NFC Forum "Text Record Type Definition" section 3.2.1.
*
* bit7 is the Text Encoding Field.
*
* if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
* The text is encoded in UTF16
*
* Bit_6 is reserved for future use and must be set to zero.
*
* Bits 5 to 0 are the length of the IANA language code.
*/
//Get the Text Encoding
String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
//Get the Language Code
int languageCodeLength = payload[0] & 0077;
String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
//Get the Text
String text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
return new TextRecord(text, languageCode);
}
catch(Exception e)
{
throw new RuntimeException("Record Parsing Failure!!");
}
Run Code Online (Sandbox Code Playgroud)