Tzuyu Lin
Tzuyu Lin

Reputation: 83

Hex to String in Android(Java)?

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 ?

Upvotes: 4

Views: 1342

Answers (3)

Kaung San
Kaung San

Reputation: 79

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]

Upvotes: 0

Tzuyu Lin
Tzuyu Lin

Reputation: 83

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

Upvotes: 0

Maroun
Maroun

Reputation: 95968

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.

Upvotes: 1

Related Questions