如何将 bytearray 转换为 String
我正在使用以下代码提取歌曲的元数据,以及如何将字节数组(buf)转换为字符串?请帮助我,提前致谢。
String mint = httpConnection.getHeaderField("icy-metaint");
int b = 0;
int count =0;
while(count++ < length){
b = inputStream.read();
}
int metalength = ((int)b)*16;
if(metalength <= 0)
return;
byte buf[] = new byte[metalength];
inputStream.read(buf,0,buf.length);
I am extracting metadata of a song using following code ,And how I can convert the byte array (buf) to string? Please help me,Thanks in advance.
String mint = httpConnection.getHeaderField("icy-metaint");
int b = 0;
int count =0;
while(count++ < length){
b = inputStream.read();
}
int metalength = ((int)b)*16;
if(metalength <= 0)
return;
byte buf[] = new byte[metalength];
inputStream.read(buf,0,buf.length);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)。从流中读取字节:
2).从字节创建字符串:
这意味着您知道从服务器发送之前对文本数据进行编码的编码。在上面的示例中,编码是 UTF-8。 BlackBerry 支持以下字符编码:
默认编码为“ISO-8859-1”。因此,当您使用
String(byte[] data)
构造函数时,它与String(byte[] data, "ISO-8859-1")
相同。如果您不知道服务器使用什么编码,那么我建议先尝试 UTF-8,因为现在它几乎已经成为服务器的默认编码。另请注意,服务器可能通过 http 标头发送编码,因此您可以从响应中提取它。然而,我看到很多服务器将“UTF-8”放入标头,而实际上使用 ISO-8859-1 甚至 ASCII 进行数据编码。
1). Read bytes from the stream:
2). Create a String from the bytes:
This implies you know the encoding the text data was encoded with before sending from the server. In the example right above the encoding is UTF-8. BlackBerry supports the following character encodings:
The default encoding is "ISO-8859-1". So when you use
String(byte[] data)
constructor it is the same asString(byte[] data, "ISO-8859-1")
.If you don't know what encoding the server uses then I'd recommend to try UTF-8 first, because by now it has almost become a default one for servers. Also note the server may send the encoding via an http header, so you can extract it from the response. However I saw a lot of servers which put "UTF-8" into the header while actually use ISO-8859-1 or even ASCII for the data encoding.
String
有一个构造函数,它接受可以用于此目的的字节数组。参见例如 http://java.sun.com/javame /reference/apis/jsr139/java/lang/String.html
String
has a constructor that accepts a byte array that you can use for this.See e.g. http://java.sun.com/javame/reference/apis/jsr139/java/lang/String.html
正如@Heiko提到的,您可以使用构造函数直接创建字符串。这也适用于黑莓java:
As @Heiko mentioned you can create string directly using the constructor. This applies to blackberry java too: