I have used the below code to convert Charsequence to Byte Array. Then I save the Byte Array as Blob to my Sqlite Database.
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.
How to convert Byte Array to Charsequence?
Any help is much appreciated.
To convert a
CharSequenceinto a byte arrayTo convert back again
Just remember that
CharSequenceis an interface that is implemented byString,StringBuilder,StringBuffer, etc so allStringinstances areCharSequenceinstances but not allCharSequenceinstances areStringbut the contract forCharSequenceis that itstoString()method should return the equivalentStringInternally 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-8orUTF-16depending on the likely encoding size of your data. Where Latin scripts predominate,will 99.9% of the time give the most space efficient encoding, for non-latin character sets (e.g. Chinese) you may find
UTF-16more 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 asUTF-8is more widely expected I recommendUTF-8as the default encoding in any case.