如何将 bytearray 转换为 String

发布于 2024-10-30 20:22:38 字数 382 浏览 2 评论 0原文

我正在使用以下代码提取歌曲的元数据,以及如何将字节数组(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-11-06 20:22:38

1)。从流中读取字节:

// use net.rim.device.api.io.IOUtilities
byte[] data = IOUtilities.streamToBytes(inputStream);

2).从字节创建字符串:

String s = new String(data, "UTF-8");

这意味着您知道从服务器发送之前对文本数据进行编码的编码。在上面的示例中,编码是 UTF-8。 BlackBerry 支持以下字符编码:

* "ISO-8859-1"
* "UTF-8"
* "UTF-16BE"
* "US-ASCII" 

默认编码为“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:

// use net.rim.device.api.io.IOUtilities
byte[] data = IOUtilities.streamToBytes(inputStream);

2). Create a String from the bytes:

String s = new String(data, "UTF-8");

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:

* "ISO-8859-1"
* "UTF-8"
* "UTF-16BE"
* "US-ASCII" 

The default encoding is "ISO-8859-1". So when you use String(byte[] data) constructor it is the same as String(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.

花间憩 2024-11-06 20:22:38

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

彩虹直至黑白 2024-11-06 20:22:38

正如@Heiko提到的,您可以使用构造函数直接创建字符串。这也适用于黑莓java:

byte[] array = {1,2,3,4,5}; 
String str = new String(array);

As @Heiko mentioned you can create string directly using the constructor. This applies to blackberry java too:

byte[] array = {1,2,3,4,5}; 
String str = new String(array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文