I want to convert my Hindi input to UTF-16 format. That's why I convert my string to byte array using character set "UTF-16".
But it will replace my string with ?????.
Here is the code
String original = "गुणवत्ता";
byte[] bytearr = original.getBytes("UTF-16");
String test= new String(bytearr,"UTF-16");
Try to encode the converted string as follows:
String original = "गुणवत्ता";
byte[] bytearr = original.getBytes("UTF-16");
String test= new String(bytearr,"UTF-16");
String encodedString = MimeUtility.encodeText(test, "utf-16", "B");
Related
First read excel as byte array, then convert this byte array to string, then convert this string to byte array again.
String fileLocation = "/tmp/a.xlsx";
byte[] bytes1 = Files.readAllBytes(Paths.get(fileLocation));
String str = new String(bytes1);
byte[] bytes2 = str.getBytes();
System.out.println(Arrays.equals(bytes1, bytes2)); // false
Why bytes1 is not equals to bytes2?
When you are converting from bytes to a String
String str = new String(bytes1);
you are potentially losing non-char bytes.
As per the javadocs
The behavior of this constructor when the given bytes are not valid in the default charset is unspecified.
I want to convert my byte[] to a String, and then convert that String to a byte[].
So,
byte[] b = myFunction();
String bstring = b.toString();
/* Here the methode to convert the bstring to byte[], and call it ser */
String deser = new String(ser);
bstring gives me [B#74e752bb.
And then convert the String to byte[]. I'm not using it in this order, but this is an example.
How do I need to do this in Java?
When converting byte[] to String, you should use this,
new String(b, "UTF-8");
instead of,
b.toString();
When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.
Your method when dealt appropriately, should be written something like this,
public static void main(String args[]) throws Exception {
byte[] b = myFunction();
// String bstring = b.toString(); // don't do this
String bstring = new String(b, "UTF-8");
byte[] ser = bstring.getBytes("UTF-8");
/* Here the methode to convert the bstring to byte[], and call it ser */
String deser = new String(ser, "UTF-8");
}
I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).
You can do it like this,
String string = "Your String";
byte[] bytesFromString = string.getBytes(); // get bytes from a String
String StringFromByteArray = new String(bytesFromString); // get the String from a byte array
I am trying to transform a string to byte and then get again as string, but the result was different. Have a look:
To decode:
byte[] tokenInBytes = Base64.decode(token, mFlags);
mTokenPreference.put(Base64.encodeToString(tokenInBytes, mFlags));
To get like a string again:
String value = Base64.encodeToString(tokenInBytes, mFlags);
The original string (before decode): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhMDA2ZTI4OGQ4ZDc1Z
And after was (after encode): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJpZCI6IjVhMDA2ZTI4OGQ4ZDc1Z
Dots was removed. Anyone knows what I did wrong?
The value of mFlags was:
int mFlags = Base64.NO_WRAP | Base64.URL_SAFE | Base64.NO_PADDING;
You can replace your string value (dot) with any letter then after encode and decode replace same letter with dot.
I got this code from this link
// Sending side
byte[] data = value.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String value = new String(data, "UTF-8");
In Java, I have a String of bits e.g. "01100111000111...". Next, I want to do the following:
convert string to byte array which I have successfully done using:
byte[] bytes = new BigInteger(bits, 2).toByteArray();
Next, I want to convert bytes to String which I tried to do using:
String byteString = new String(bytes, "UTF-8");
but the results are not correct (garbage characters etc.).
I think "UTF-8" is not the proper encoding.
Kindly tell if there is any other way to get the string from such bytes or the proper encoding.
Edited after your comment:
String string = "01100111000111";
byte[] bytes = new BigInteger(string, 2).toByteArray();
String out = "";
for(byte b: bytes)
out+= String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
System.out.println(out);
output:
0001100111000111
Hope this can help.
I have this string "\U05d0\U05d5\U05d2\U05e0\U05d3\U05d4","\U05d0\U05d5\U05d6\U05d1\U05e7\U05d9\U05e1\U05d8\U05df","\U05d0\U05d5\U05e1\U05d8\U05e8\U05d9\U05d4"
how do i convert it to a readable string? (note this is supposed to be hebrew)
i tried this method but it didnt work
byte[] bytes = s.getBytes();
String decoded = new String(bytes);
System.out.println(decoded);
All U should be lowercase u:
String s = "\u05d0\u05d5\u05d2\u05e0\u05d3\u05d4";
try{
byte[] bytes = s.getBytes();
String decoded = new String(bytes);
System.out.println(decoded);
} catch(UnsupportedEncodingException e) {
// ...
}
See Byte Encodings and Strings.
Output:
אוגנדה