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 CharSequence into a byte array
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
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,
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.
It looks like you are using ASCII data (if not, your code is quite lossy).
To get a CharSequence from ASCII bytes, you can do
CharSequence x = new String(theBytes, "US-ASCII");
For other encodings, just specify the name of the character set.
CharSequence c = new String(byte[]);
Related
I am trying to determine if an in-house method will decode a byte array correctly given different encodings. The following code is how I approached generating data to encode.
public class Encoding {
static byte[] VALUES = {(byte) 0x00, ..... (byte) 0xFF};
static String[] ENCODING = {"Windows-1252","ISO-8859-1"};
public static void main(String[] args) throws UnsupportedEncodingException {
for(String encode : ENCODING) {
for(byte value : VALUES) {
byte[] inputByte = new byte[]{value};
String input = new String(inputByte, encode);
String houseInput = houseMethod(input.getBytes());
}
}
}
}
My question is when it comes making the call to the house method, what encoding will it send to that method? It is my understanding when Java stores a String, it converts it to UTF-16. So when I am sending Input.getBytes(), is it sending the UTF-16 encoding byte or the encoding scheme that I set when I created a new String? I am guessing that it is UTF-16, but I am not sure. Should the house method be???
houseMethod(input.getBytes(encode))
See String.getBytes():
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
You are well advised to use the String.getBytes(Charset) method instead and explicitly specify the desired encoding.
As per Java documentation String.getBytes():
Encodes this String into a sequence of bytes using the platform's
default charset, storing the result into a new byte array
So the bytes that the in house method gets depend on which OS you are, as well as your locale settings.
OTH, String.getBytes(encoding) ensures you get the bytes in the encoding you pass as parameter.
I need to convert the character unicode to a byte[] representation and save into Srting, for example
U+1F601 -> \xF0\x9F\x98\x81
I dont have idea how can i do it this..
Anyone has idea?Thanks
int[] codepoints = { 0x1F601 }; // U+1F601
String s = new String(codepoints, 0, codepoints.length);
byte[] bytes = s.getBytes(StandardCharsets.UTF_8); // As UTF-8 (Unicode) bytes
System.out.println(Arrays.toString(bytes));
So one first coposes the Unicode code points into a java String. Java Strings hold Unicode.
When one wants bytes, say in UTF-8 - a Unicode representation -, then one has to indicate the CharSet in which the bytes will be.
Here below is how I generate a SecureRandom:
byte[] arr = SecureRandom.getInstance("SHA1PRNG").generateSeed(32);
then, I convert it to a string like this:
String str = new String(arr)
and finally, I try to convert the string back to my original byte array:
byte[] arr2 = str.getBytes()
The problem is that the last statement does not return my original byte array... Am I missing something?
then, I convert it to a string like this:
Don't do that!
You have two problems here:
this constructor will use the default encoding;
even if you used UTF-8 as an encoding, some byte sequences just cannot be encoded to chars!
You should not use String to hold binary data; or use a string-based encoding, such as Base64.
For more information, see CharsetDecoder and CodingErrorAction.
Is it possible to convert a byte array to a string but where the length of the string is exactly the same length as the number of bytes in the array? If I use the following:
byte[] data; // Fill it with data
data.toString();
The length of the string is different than the length of the array. I believe that this is because Java and/or Android takes some kind of default encoding into account. The values in the array can be negative as well. Theoretically it should be possible to convert any byte to some character. I guess I need to figure out how to specify an encoding that generates a fixed single byte width for each character.
EDIT:
I tried the following but it didn't work:
byte[] textArray; // Fill this with some text.
String textString = new String(textArray, "ASCII");
textArray = textString.getBytes("ASCII"); // textArray ends up with different data.
You can use the String constructor String(byte[] data) to create a string from the byte array. If you want to specify the charset as well, you can use String(byte[] data, Charset charset) constructor.
Try your code sample with US-ASCII or ISO-8859-1 in place of ASCII. ASCII is not a built-in Character encoding for Java or Android, but one of those two are. They are guaranteed single-byte encodings, with a caveat that characters not in the character set will be silently truncated.
This should work fine!
public static byte[] stringToByteArray(String pStringValue){
int length= pStringValue.length();
byte[] bytes = new byte[length];
for(int index=0; index<length; index++){
char ch= pStringValue.charAt(index);
bytes[index]= (byte)ch;
}
return bytes;
}
since JDK 1.6:
You can also use:
stringValue.getBytes() which will return you a byte array.
In case of passing a NULL string, you need to handle that by either throwing the nullPointerException or handling it inside the method itself.
This is simply to error check my code, but I would like to convert a single byte out of a byte array to a string. Does anyone know how to do this? This is what I have so far:
recBuf = read( 5 );
Log.i( TAG, (String)recBuf[0] );
But of course this doesn't work.
I have googled around a bit but have only found ways to convert an entire byte[] array to a string...
new String( recBuf );
I know I could just do that, and then sift through the string, but it would make my task easier if I knew how to operate this way.
You can make a new byte array with a single byte:
new String(new byte[] { recBuf[0] })
Use toString method of Byte
String s=Byte.toString(recBuf[0] );
Try above , it works.
Example:
byte b=14;
String s=Byte.toString(b );
System.out.println("String value="+ s);
Output:
String value=14
There's a String constructor of the form String(byte[] bytes, int offset, int length). You can always use that for your conversion.
So, for example:
byte[] bite = new byte[]{65,67,68};
for(int index = 0; index < bite.length; index++)
System.out.println(new String(bite, index,1));
What about converting it to char? or simply
new String(buffer[0])
public static String toString (byte value)
Since: API Level 1
Returns a string containing a concise, human-readable description of the specified byte value.
Parameters
value the byte to convert to a string.
Returns
a printable representation of value.]1
this is how you can convert single byte to string try code as per your requirement
Edit:
Hows about
""+ recBuf[0];//Hacky... not sure if would work
((Byte)recBuf[0]).toString();
Pretty sure that would work.
Another alternate could be converting byte to char and finally string
Log.i(TAG, Character.toString((char) recBuf[0]));
Or
Log.i(TAG, String.valueOf((char) recBuf[0]));
You're assuming that you're using 8bit character encoding (like ASCII) and this would be wrong for many others.
But with your assumption you might just as well using simple cast to character like
char yourChar = (char) yourByte;
or if really need String:
String string = String.valueOf((char)yourByte);