简体   繁体   English

十六进制到字符串在Android(Java)?

[英]Hex to String in Android(Java)?

Maybe I was sticky in hex to String?I don't know. 也许我对String不满意? my code: 我的代码:

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

txValue should be byte ? txValue应该是字节吗?

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 ? 来人帮帮我 ?

You should use public String(byte[] bytes) constructor: 您应该使用public String(byte[] bytes)构造函数:

Constructs a new String by decoding the specified array of bytes using the platform's default charset. 通过使用平台的默认字符集解码指定的字节数组来构造新的String。 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的长度是字符集的函数,因此可能不等于字节数组的长度。

String s = new String(txValue);

and then print s , it contains what you want. 然后打印s ,其中包含您想要的内容。

Printing txValue and txValue.toString() will print it in byte format. 打印txValuetxValue.toString()将以字节格式打印。

I find the way: final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA); 我找到了方法:最终字节[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);

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

String text = Integer.toString(GasValue); 字符串文本= Integer.toString(GasValue);

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

OK

You should use Arrays.toString(txValue) 您应该使用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] 结果如下[27,0,1,13,13,4,5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM