byte [] toString()给出一个奇怪的字符串,而不是实际值 [英] byte[] toString() gives a weird string instead of actual value

查看:60
本文介绍了byte [] toString()给出一个奇怪的字符串,而不是实际值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

byte [] a 的值为{119},与"w"的ascii等效,但是当我使用 .toString()将其转换为字符串,它给了我一个奇怪的字符串.知道我做错了什么吗?

  byte [] a = featureRX.getValue();字符串rscvString = a.toString();Log.d("byteToHex","rscvString =" + rscvString);while(rscvString!="w"){ 

解决方案

字符串对象采用参数 byte [] 作为重载构造函数.使用 String rscvString = new String(a); ,应该对您进行排序

您不能使用布尔运算符对字符串进行测试,即.!=或==.使用 while(!(rscvString.equalsIgnoreCase("w"))) equalsIgnoreCase()方法将返回一个布尔值,而!将强制对假进行测试.

byte[] a has value of {119}, which is the ascii equivalent of "w", but when I use .toString() to convert it to string, it gives me a weird string. any idea what I did wrong?

byte[] a = characteristicRX.getValue();
        String rscvString = a.toString();
        Log.d("byteToHex", "rscvString = " + rscvString);
        while ( rscvString != "w" ){

解决方案

String object takes a parameter of byte[] as an overloaded constructor. Use String rscvString = new String(a); and you should be sorted

You can't use boolean operators to test against strings ie. != or ==. use while ( !(rscvString.equalsIgnoreCase("w") ) the equalsIgnoreCase() method will return a boolean and the ! will force the test against the false.

这篇关于byte [] toString()给出一个奇怪的字符串,而不是实际值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆