2012-07-24 18 views
0

我試圖獲取EditText框的值並將其轉換爲用於nfc編寫的字節數組。基本上,我想只能從EditText框中寫入NFC標籤。我試過簡單的東西,如.toString()和UTF-8編碼,但我的問題似乎是從可編輯的獲取字符串值。任何人都有經驗嗎?將可編輯(從EditText)轉換爲字節數組

編輯:相關代碼在這裏:

EditText msgInput = (EditText) findViewById(R.id.editText1); 

    ... 

String msg = msgInput.getText().toString(); 
Log.i(TAG, "msg: " + msg); 

byte[] bytes = msg.getBytes(); 
Log.i(TAG, "bytes: " + bytes); 

messagePayload = bytes; 

我進入 '東西'(不帶引號)到我的EditText,然後試着寫。這給出了兩個logcat的輸出:

msg: stuff 
    bytes: [[email protected] 
+0

'的toString()'應該返回的你'Editable'就好字符串表示。你能對你的問題更具體嗎? – CommonsWare 2012-07-24 16:28:09

+0

對不起 - 爲了toString()不起作用,toString()返回類似'[B'和其他一些垃圾,作爲算法的一部分。顯示爲字符串時正常工作,轉換爲其他格式時效果不佳。 – alextoombs 2012-07-24 16:30:30

+0

嘗試首先使用string.toCharArray()轉換爲char數組() – 2012-07-24 16:43:38

回答

3

也許,

editBox.getText().toString().getBytes() 
+0

不引用工作 - 檢查我的編輯。 – alextoombs 2012-07-24 16:51:18

+1

@atoombs Log.i(TAG,「bytes:」+ bytes)是錯誤的,請使用Log.i(TAG,Arrays.toString(bytes)) – 2012-07-24 16:53:41

+0

顯示字節 - 感謝您的幫助。我想我只是不知道爲什麼消息的字節在嘗試使用該字節數組創建新的NdefMessage時拋出FormatException。 – alextoombs 2012-07-24 17:01:55