java 在Android中将字节数组转换为字符序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12052050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Byte Array to Charsequence in Android
提问by Andro Selva
I have used the below code to convert Charsequence to Byte Array. Then I save the Byte Array as Blob to my Sqlite Database.
我使用以下代码将 Charsequence 转换为字节数组。然后我将字节数组作为 Blob 保存到我的 Sqlite 数据库中。
For this , I have used the below code,
为此,我使用了以下代码,
public static byte[] toByteArray(CharSequence charSequence) {
if (charSequence == null) {
return null;
}
byte[] barr = new byte[charSequence.length()];
for (int i = 0; i < barr.length; i++) {
barr[i] = (byte) charSequence.charAt(i);
}
return barr;
}
Now I would like to convert my byte array retrieved from sqlite to Charsequence. But I couldn't get any help on it.
现在我想将从 sqlite 检索到的字节数组转换为 Charsequence。但我无法得到任何帮助。
How to convert Byte Array to Charsequence?
如何将字节数组转换为字符序列?
Any help is much appreciated.
任何帮助深表感谢。
回答by Stephen Connolly
To convert a CharSequence
into a byte array
将 aCharSequence
转换为字节数组
CharSequence seq;
Charset charset;
...
byte[] bytes = seq.toString().getBytes(charset);
To convert back again
再次转换回来
CharSequence seq2 = new String(bytes, charset);
Just remember that CharSequence
is an interface that is implemented by String
, StringBuilder
, StringBuffer
, etc so all String
instances are CharSequence
instances but not all CharSequence
instances are String
but the contract for CharSequence
is that its toString()
method should return the equivalent String
请记住,这CharSequence
是一个由String
, StringBuilder
,StringBuffer
等实现的接口,因此所有String
实例都是CharSequence
实例,但并非所有CharSequence
实例都是实例,String
但约定CharSequence
是其toString()
方法应返回等效项String
Internally all strings in Java are represented as Unicode, so as long as the consumer and producer are both Java the safest charset to use is one of UTF-8
or UTF-16
depending on the likely encoding size of your data. Where Latin scripts predominate,
在内部,Java 中的所有字符串都表示为 Unicode,因此只要消费者和生产者都是 Java,使用的最安全字符集是数据的可能编码大小之一UTF-8
或UTF-16
取决于数据的可能编码大小。在拉丁文字占主导地位的地方,
Charset charset = Charset.forName("UTF-8");
will 99.9% of the time give the most space efficient encoding, for non-latin character sets (e.g. Chinese) you may find UTF-16
more space efficient depending on the data set you are encoding. You would need to have measurements showing that it is a more space efficient encoding and as UTF-8
is more widely expected I recommend UTF-8
as the default encoding in any case.
将 99.9% 的时间提供最节省空间的编码,对于非拉丁字符集(例如中文),您可能会发现UTF-16
更多的空间效率取决于您编码的数据集。您需要进行测量以证明它是一种更节省空间的编码,并且正如UTF-8
更广泛预期的那样,我建议UTF-8
在任何情况下将其作为默认编码。
回答by Thilo
It looks like you are using ASCII data (if not, your code is quite lossy).
看起来您正在使用 ASCII 数据(如果不是,则您的代码非常有损)。
To get a CharSequence from ASCII bytes, you can do
要从 ASCII 字节获取 CharSequence,您可以执行
CharSequence x = new String(theBytes, "US-ASCII");
For other encodings, just specify the name of the character set.
对于其他编码,只需指定字符集的名称。
回答by Dan
CharSequence c = new String(byte[]);