My input is "[B#ec3c95b"
String example ="[B#ec3c95b";
I want to make it same as output "[B#ec3c95b" in byte array type.
If I understand, you want to convert a String in a byte array.
For that, you can use the getBytes() function to convert your String :
String input = "[B#ec3c95b";
byte[] output = input.getBytes();
Related
How can I convert a word's HEX code string to Shift JIS encoding?
For example, I have a string:
"90DD92E882F08F898AFA89BB82B582DC82B782A9"
And I want to get the following output:
設定を初期化しますか
String s = new String(new BigInteger("90DD92E882F08F898AFA89BB82B582DC82B782A9", 16).toByteArray(), "Shift_JIS");
will do it for you for earlier versions
Assuming you have Java 17+, which added java.util.HexFormat, then you can use parseHex followed by a conversion from the byte array to a string:
byte[] bytes = HexFormat.of().parseHex("90DD92E882F08F898AFA89BB82B582DC82B782A9");
String str = new String(bytes, "Shift_JIS");
If you do not have Java 17+, then the related answer I linked to gives an alternative approach instead of parseHex.
I don't have the correct charset/font to show the result in my console, but here is the str variable in my debugger:
I want to conver BloomFilter to String, store it and then get it from String.
If I do it using just byte array, without converting to String - everything is ok:
BloomFilter<Integer> filter = BloomFilter.create(
Funnels.integerFunnel(),
500,
0.01);
for (int i=0; i<400; i++) {
filter.put(i);
}
System.out.println(filter.approximateElementCount());
System.out.println(filter.expectedFpp());
String s = "";
ByteArrayOutputStream out = new ByteArrayOutputStream();
filter.writeTo(out);
s = out.toString(Charset.defaultCharset());
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
BloomFilter<Integer> filter1 = BloomFilter.readFrom(in, Funnels.integerFunnel());
System.out.println(filter1.approximateElementCount());
System.out.println(filter1.expectedFpp());
I get equals output, but if I convert bytes to String and then String to bytes - the result is wrong, I get filter1.approximateElementCount() = 799 instead of 402.
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes(Charset.defaultCharset()));
Is there a way to convert BloomFilter to String and back?
Converting bytes to a String and back is not always reversible in any Charset. You must use a tool such as Base64 (provided in Guava as BaseEncoding.base64()) to convert a byte array to a string in such a way that you can always convert it back correctly.
I receive json data that contains binary data like that ,and I would like to convert that data to byte[] in java but I don't know how ?.
"payload": "7V1bcxs3ln6frfdcfvfbghfdX8HSw9Zu1QzzartyhblfdcvberCObjvJpkiJUpmhRI1pKXYeXHRsZLSrCy
5dElN5tfvQaO72TdSoiOS3TH8Yxdffgtg754679513qdfrgvlslsqdeqaepdccngrdzedrtghBD+d++e7v//p80/v96v7h+u72
+z1gfK/39x/+9t391cPTzeP88aE/++Fvvd53n+8+Xd1c/fBm/unqAf+7
N7v65en++vGP3vx2fvPHw/XDdwfpHf5mevhq/vQDcnAAwD+gEPwDF+bDxTv+3UF61d/4eesrfP356uFx"
Based on the observation that the "binary" string consists of ASCII letters, digits and "+" and "/", I am fairly confident that it is actually Base64 encoded data.
To decode Base64 to a byte[] you can do something like this:
String s = "7V1bcxs3ln6...";
byte [] bytes = java.util.Base64.getDecoder().decode(s);
The decode call will throw IllegalArgumentException if the input string is not properly Base64 encoded.
When I decoded that particular string using an online Base64 decoder, the result is unintelligible. But that is what I would expect for an arbitrary "blob" of binary data.
In general if you have a String in some object that denotes the json payload you can :
String s = "7V1bcxs3ln6...";
byte [] bytes = s.getBytes();
Other than that if this payload should be decoded somehow then additional code will be required.
In my case I had to convert payload that I knew it was a text something like:
{"payload":"eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6IngiLCJjaXR5IjoiaGVyZSJ9"}
This is the difference between java.util.Base64.getDecoder() and getBytes():
String s = "eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6IngiLCJjaXR5IjoiaGVyZSJ9";
byte [] bytes = s.getBytes();
byte [] bytes_base64 = java.util.Base64.getDecoder().decode(s);
String bytesToStr = new String(bytes, StandardCharsets.UTF_8);
String bytesBase64Tostr = new String(bytes_base64, StandardCharsets.UTF_8);
System.out.println("bytesToStr="+bytesToStr);
System.out.println("bytesBase64Tostr="+bytesBase64Tostr);
Output:
bytesToStr=eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6IngiLCJjaXR5IjoiaGVyZSJ9
bytesBase64Tostr={"user_id":4,"username":"x","city":"here"}
java.util.Base64.getDecoder() worked for in my case
I need a solution to convert String to byte array without changing like this:
Input:
String s="Test";
Output:
String s="Test";
byte[] b="Test";
When I use
s.getBytes();
then the reply is
"[B#428b76b8"
but I want the reply to be
"Test"
You should always make sure serialization and deserialization are using the same character set, this maps characters to byte sequences and vice versa. By default String.getBytes() and new String(bytes) uses the default character set which could be Locale specific.
Use the getBytes(Charset) overload
byte[] bytes = s.getBytes(Charset.forName("UTF-8"));
Use the new String(bytes, Charset) constructor
String andBackAgain = new String(bytes, Charset.forName("UTF-8"));
Also Java 7 added the java.nio.charset.StandardCharsets class, so you don't need to use dodgy String constants anymore
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
String andBackAgain = new String(bytes, StandardCharsets.UTF_8);
You can revert back using
String originalString = new String(b, "UTF-8");
That should get you back your original string. You don't want the bytes printed out directly.
You may try the following code snippet -
String string = "Sample String";
byte[] byteArray = string.getBytes();
In general that's probably not what you want to do, unless you're serializing or transmitting the data. Also, Java strings are UTF-16 rather than UTF-8, which what more like what you're expecting. If you really do want/need this then this should work:
String str = "Test";
byte[] raw = str.getBytes(new Charset("UTF-8", null));
I have a program which reads a message from MQ. the character set is 1047. Since my java version is very old it doesn't support thus character set.
Is it possible to change this string into char set 500 in the program after receiving but before reading.
For eg:
public void fun (String str){ //str in char set 1047. **1047 is not supported in my system**
/* can I convert str into char set 500 here. Convert it into byte stream and then back to string. Something like this */
byte [] b=str.getBytes();
ByteArrayOutputStream baos = null;
try{
baos = new ByteArrayOutputStream();
baos.write(b);
String str = baos.toString("IBM-500");
System.out.println(str);
}
byte [] b=str.getBytes(); //will convert string(encoding could only be Unicode in jvm) to bytes using file.encoding. You should check whether the str contains correct information, if so, you need not care the 1047 encoding, just run str.getBytes("IBM-500"), you will get the 500 encoded bytes. Again, String object only use Unicode, if you convert string to bytes, the encoding matters the result bytes array.