字节数组为字符串的Andr​​oid [英] byte array to String in Android

查看:115
本文介绍了字节数组为字符串的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来一个byte []转换为字符串而无需创建一个新的String对象。

I need a way to convert a byte[] to a String without creating a new String object.

我不想要什么:

一个String =新的字符串(字节[],诠释抵消,诠释BYTECOUNT);

有另一种方式来达到同样的事情,而无需创建一个新的String对象?

Is there another way to achieve the same thing without creating a new String object?

我一直在寻找的Base64类。这是否工作?

I've been looking at Base64 class. Does that work?

推荐答案

这只会如果一个地方你必须从你会得到每个输入生成的字符串的高速缓存是不可能的。

This would only be possible if somewhere you had a cache of the strings generated from every input you're going to get.

字符串是不变的,所以你不能把新的数据到的现有的字符串 - 所以,除非你可以的找到的已具有正确的数据,现有的字符串(我之前提到的)高速缓存,你必须创建一个新的字符串。

Strings are immutable, so you can't put the new data into an existing string - so unless you can find an existing string which already has the right data (the cache I mentioned before) you'll have to create a new string.

(我也强烈建议您指定要使用的,而不是依靠系统默认编码的字符编码​​。)

(I would also strongly recommend that you specify the character encoding you want to use, instead of relying on the system default encoding.)

当然,如果这不是真正的EN codeD文本入手(例如,如果字节[] 数据是从图像),那么会的Base64更适合反正 - 但这是从你是否需要一个新的字符串正交

Of course, if this isn't genuinely encoded text to start with (e.g. if the byte[] data is from an image) then Base64 would be more appropriate anyway - but that's orthogonal from whether or not you need a new string.

这篇关于字节数组为字符串的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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