Convert byte array to charsequence in Android

I used the code below to convert Charsequence to a byte array. Then I save the byte array as Blob in my Sqlite database.

For this, I used the code below

 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 a byte array obtained from sqlite to Charsequence. But I could not help.

How to convert byte array to charsequence?

Any help is greatly appreciated.

+5
source share
3 answers

Convert a CharSequenceto byte array

CharSequence seq;
Charset charset;
...
byte[] bytes = seq.toString().getBytes(charset);

To reverse backward

CharSequence seq2 = new String(bytes, charset);

, CharSequence - , String, StringBuilder, StringBuffer .., String CharSequence, CharSequence String CharSequence , toString() String

Java Unicode, , Java, - UTF-8 UTF-16 . ,

Charset charset = Charset.forName("UTF-8"); 

99,9% , - (, ) UTF-16 , . , , , UTF-8 , UTF-8 .

+14

, ASCII ( , ).

CharSequence ASCII-,

CharSequence x = new String(theBytes, "US-ASCII");

.

+7
CharSequence c = new String(byte[]);
+1
source

All Articles