4

Maybe I was sticky in hex to String?I don't know. my code:

final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);

txValue should be byte ?

debug:

Log.d("p1", ""+txValue.toString());

then show me those:

[B@1e631929
[B@9264ae

I don't know how to fix it ? somebody help me ?

earthmover
  • 4,395
  • 10
  • 43
  • 74
Tzuyu Lin
  • 83
  • 7

3 Answers3

1

You should use public String(byte[] bytes) constructor:

Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

String s = new String(txValue);

and then print s, it contains what you want.

Printing txValue and txValue.toString() will print it in byte format.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I did it before,but it's not working.Still show [B@367d19dc,I found that data maybe like this: b11011011 , and I had to find out the way can show correct data from final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA); – Tzuyu Lin Apr 21 '15 at 02:17
0

I find the way: final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);

final int GasValue = ((txValue[0]<<8)|(txValue[1]&0xff))&0xffff;

String text = Integer.toString(GasValue);

Log.d("p1", ""+text);

OK

Tzuyu Lin
  • 83
  • 7
0

You should use Arrays.toString(txValue)

This is how I use i code

 final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);
   txtResult.setText(Arrays.toString(txValue));

Result is like below [27,0,1,13,13,4,5]

Kaung San
  • 79
  • 1
  • 14