Java 如何将字节数组转换为字符串?
我试图将从蓝牙设备读取的字节转换成字符串。下面是我正在使用的代码,但它没有给出正确的字符串。 输出是这样的 02-19 18:01:49.300:I/BluetoothReadService(17693):消息内容如下:���8.��1.04TTO��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Java 如何将字节数组转换为字符串?,java,android,bluetooth,Java,Android,Bluetooth,我试图将从蓝牙设备读取的字节转换成字符串。下面是我正在使用的代码,但它没有给出正确的字符串。 输出是这样的 02-19 18:01:49.300:I/BluetoothReadService(17693):消息内容如下:���8.��1.04TTO������������������������������������������������������������������������������������������������������������������������������
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true)
{
try
{
// Read from the InputStream
bytes = mmInStream.read(buffer, 0, buffer.length);
String readMessage = null;
byte[] getBytes = null;
try {
readMessage = BitConverter.toString(buffer);
getBytes = BitConverter.getBytes(readMessage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s = new String(getBytes, "US-ASCII");
Log.i(TAG, "Message Read : "+readMessage +"ByteString"+s);
mHandler.obtainMessage(MainActivity.MESSAGE_READ).sendToTarget();
} catch (IOException e)
{
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
请建议如何操作?readMessage字符串应以不同的方式初始化。
如果使用
readMessage=新字符串(缓冲区)代码>,它应该可以工作。通过ApacheStringUtils
您可以使用此方法
如果问题仍然存在,请使用下面的方法读取字符串
public String readData() {
String response = null;
int bytesReceived=-1;
byte[] readBuffer = new byte[1024];
try{
try{
bytesReceived = inputStream.read(readBuffer);
Logger.debugLog(MODULE ,"# Byte Receieved #" + bytesReceived);
}catch(Exception e){
Logger.errorLog(MODULE ,"Error in readData() , Reason:"+e.getMessage(),e);
}
if(bytesReceived > 0){
response = new String(readBuffer,0,bytesReceived);
Logger.debugLog(MODULE + "Total Bytes Received: " + bytesReceived);
Logger.debugLog(MODULE, "Total Time Taken : " + timetoread);
Logger.debugLog(MODULE, "Length of data received : " + response.length());
Logger.infoLog(MODULE, "Data Received : ####" + response + "####");
}else{
////////// HERE MEANS TCP CONNECTION STATUS WILL CLOSE_WAIT
////////// SO RELEASING CONNECTION.
Logger.infoLog(MODULE, " Releasing Resource. bytesReceived is <= 0 : Total Bytes Received :"+ bytesReceived );
releaseResources();
}
Logger.infoLog(MODULE, " Resource has been released.");
}
}catch(Exception e){
Logger.errorLog(MODULE, "In catch : Data Received : ####" + response + "####");
Logger.errorLog(MODULE, "Exception in readdata() : " + e);
}finally{
readBuffer=null;
}
return response;
}
公共字符串readData(){
字符串响应=null;
int字节接收=-1;
字节[]读缓冲区=新字节[1024];
试一试{
试一试{
bytesReceived=inputStream.read(读取缓冲区);
Logger.debugLog(模块,“#字节接收#”+字节接收);
}捕获(例外e){
Logger.errorLog(模块,“readData()中的错误,原因:”+e.getMessage(),e);
}
如果(bytesReceived>0){
响应=新字符串(readBuffer,0,bytesReceived);
Logger.debugLog(模块+“接收的总字节数:”+接收的字节数);
Logger.debugLog(模块,“所用总时间:“+timetoread”);
Logger.debugLog(模块,“接收数据的长度:”+response.Length());
infoLog(模块,“接收到的数据:#####“+响应+”###“);
}否则{
//////////这里表示TCP连接状态将关闭\u等待
//////////所以释放连接。
infoLog(模块,“Releasing Resource.bytesReceived”是而不是
String s = new String(getBytes, "US-ASCII");
你应该使用
String s = new String(buffer, "US-ASCII");
使用BitConverter删除该部分。您似乎忽略了实际读取的字节数,并从1024字节的整个缓冲区中创建了新字符串
请尝试以下方法:
String readMessage = new String(buffer, 0, bytes, "US-ASCII");
阅读Android官方网站,在BluetoothChatFrangment.java
中,您可以看到:
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
在本例中,msg.arg1
在您的情况下等于bytes
。另外,readBuf
是byte[]
这应该可以帮您完成这项工作
String s = new String(buffer, StandardCharsets.UTF_8)
需要KitKat以上的Api。谷歌是你的朋友。你确定字节包含美国ASCII编码的文本吗?使用此String str=new String(bytes,Charsets.US_ASCII);
位转换器做什么以及为什么要使用它?不,我已经尝试了所有这些,但得到了相同的结果。你能尝试一下String readMessage=Arrays.toString吗(缓冲区);
?它表示字节的数值。可能只是它们超出了普通字符串字符的范围。您可以这样比较它们吗?
String s = new String(buffer, StandardCharsets.UTF_8)