我正在使用Socket連接。我正在客戶端工作。 我已經通過這個討論Socket pass value as Hex。我需要發送字符串例如(0x01是一個十六進制值和一個字符串「Ravi」)在服務器,他們期待hexa值像1 72 61 76 69.我嘗試將String Ravi轉換爲六進制值作爲字符串,並附加「1 「並嘗試轉換爲字節數組。我得到一個異常,即StringIndexOutOfBound異常。在Socket通信中寫入十六進制字符串android
更新:
`公共靜態字節[] hexStringToByteArray(一個String){ INT LEN = s.length(); byte [] data = new byte [len/2];數據[i/2] =(字節)((Character.digit(s.charAt(i),16)< < 2) + (int i = 0; i < len; i + = 2) + Character.digit(s.charAt(i + 1),16)); } 返回數據; }
public String toHex(String arg) {
return String.format("%x", new BigInteger(arg.getBytes()));
}`
我用這兩種方法的1Ravi字符串轉換爲字節數組,但我得到異常hexstringtobytearray方法。
請告訴我們你已經嘗試的事情,包括樣本輸入。請注意,你並不是真的想用「十六進制」發送它 - 你只是想發送字節。 –