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
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 need a byte array, but the requirement is like first I need to convert the input stream to base64 and then the base64 to byte array.
I have tried directly to the byte array, but the requirement is like need to convert the InputStream to base64 and then byte[].
InputStream input = ....
byte[] byteArray = IOUtils.toByteArray(input);
You can use Base64 from java.util package to encode your stream to base 64
Something like this:
String initialString = "original text";
InputStream input = new ByteArrayInputStream(initialString.getBytes());
byte[] byteEncoded = Base64.getEncoder().encode(IOUtils.toByteArray(input));
The method is Base64.getEncoder().encode and have 3 candidates:
public byte[] encode(byte[] src
public int encode(byte[] src,byte[] dst)
public ByteBuffer encode(ByteBuffer buffer)
Hoping that help
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");
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 am using the JNCryptor library to encrypt a string before sending it to my server as an encrypted string. Here is my code:
String teststring = "Hello World";
JNCryptor cryptor = new AES256JNCryptor();
byte[] plaintext = teststring.getBytes();
String password = "test";
try {
byte[] ciphertext = cryptor.encryptData(plaintext, password.toCharArray());
String a = new String(ciphertext);
return a;
} catch (CryptorException e) {
// Something went wrong
e.printStackTrace();
return "0";
}
However, when I send my string "a" to the server, it has a bunch of unrecognizable characters. I read an explanation
regarding this:
String is not a suitable container for binary data and ciphertext is
binary data. For any given character encoding not all bytes and byte
sequences represents characters and when an un-representable byte or
sequence is found it is converted to some error character. Obviously
this error character cannot be converted back to a unique byte or byte
sequence (it is a many->one mapping).
Is this advice correct? In that case, how do I convert the byte[] to a string correctly? So that I can readably store it on my server?
There is no standard way for converting from a byte array to a string. You have to encode the byte array. A common way to do this is base64 encoding.
For an explanation of how base64 encoding works: http://en.wikipedia.org/wiki/Base64
Then once it gets to your server, base64 decode it back into your original byte array and store it, done!