我想顯示我的虛擬數據的前 4 個字符。我怎么做?我想要作為輸出:outputvalue = $*outputvalue = 22protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); byte[] outData = ("$*220000000000101SDFRGGHYB0").getBytes(); ByteBuffer b = ByteBuffer.wrap(outData); System.out.println("The structure"+ b); outputvalue1 = $* outputvalue2 = 22}
1 回答

手掌心
TA貢獻1942條經驗 獲得超3個贊
byte[] outData = "$*220000000000101SDFRGGHYB0".getBytes("UTF-8");
ByteBuffer b = ByteBuffer.wrap(outData);
byte[] outputValue1 = new byte[2];
b.get(outputValue1);
byte[] outputValue2 = new byte[2];
b.get(outputValue2);
String outputString1 = new String(outputValue1, "UTF-8");
String outputString2 = new String(outputValue2, "UTF-8");
int outputNum2 = Integer.parseInt(outputString2);
字符串始終采用 Unicode,而作為文本的二進制數據 (byte[]) 具有某種編碼、字符集。String/Reader/Writer在和之間轉換時必須提到這一點byte[]/InputStream/OutputStream
ByteBuffer允許定位、隨機訪問讀寫。以上使用線性讀數。
添加回答
舉報
0/150
提交
取消