I want to decode a string to UTF-8 format in java. In the following code, I am able to get the expected decoded value from String decoded.
String s = "SFSFSFSFSF";
Base32 codec = new Base32();
String encoded = codec.encodeAsString(s.toUpperCase().getBytes());
Charset UTF_8 = Charset.forName("UTF-8");
String decoded =new String(codec.decode(encoded),UTF_8); // SFSFSFSFSF
String decoded2 = codec.decode(encoded); // [B#133314b
However, in my implementation specifically, I want to get the decoded val "SFSFSFSFSF" just by saying codec.decode(encoded) which is demonstrated in String decoded2 in the code. I want to know how I can achieve this by changing the way the String is encoded.
Related
Decoding with base64 an unencoded string on Android does not gives any error but returns a string with some special characters e.g encoded like.
It should throw some IllegalArgumentException. Is there some native way in android to check that other than regex ?
private String decodeThisString = "I am a java String";
bytes[] deocdedBytes = Base64.decode(decodeThisString.getBytes(), Base64.DEFAULT);
I think you do not need to remove the character when you will decode it, automatically they will be discarded at the time of decode. I have tested with encoding and decoding with the provided code and get the exact string after decode.
String decodeThisString = "I am a java String";
//encode
byte[] data = Base64.encode(decodeThisString.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);
//decode
byte[] datas = Base64.decode(text, Base64.DEFAULT);
String texts = new String(datas, StandardCharsets.UTF_8);
I am trying to decode data in an xml format into bytes base64 and I am having an issues. My method is in java which takes a String data and converts it into bytes like as bellow.
String data = "......"; //string of data in xml format
byte[] dataBytes = Base64.getDecoder().decode(data);
Which failed and gave the exception like bellow.
java.lang.IllegalArgumentException: Illegal base64 character 3c
at java.util.Base64$Decoder.decode0(Base64.java:714)
at java.util.Base64$Decoder.decode(Base64.java:526)
at java.util.Base64$Decoder.decode(Base64.java:549)
at XmlReader.main(XmlReader.java:61)
Is the xml format not compatible with base64?
Just use this method
getMimeDecoder()
String data = "......";
byte[] dataBytes = Base64.getMimeDecoder().decode(data);
I got this same error and problem was that the string was starting with data:image/png;base64, ...
The solution was:
byte[] imgBytes = Base64.getMimeDecoder().decode(imgBase64.split(",")[1]);
You should first get the bytes out of the string (in some character encoding).
For these bytes you use the encoder to create the Base64 representation for that bytes.
This Base64 string can then be decoded back to bytes and with the same encoding you convert these bytes to a string.
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
final String xml = "<root-node><sub-node/></root-node>";
final byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
final String xmlBase64 = Base64.getEncoder().encodeToString(xmlBytes);
System.out.println(xml);
System.out.println(xmlBase64);
final byte[] xmlBytesDecoded = Base64.getDecoder().decode(xmlBase64);
final String xmlDecoded = new String(xmlBytesDecoded, StandardCharsets.UTF_8);
System.out.println(xmlDecoded);
}
}
Output is:
<root-node><sub-node/></root-node>
PHJvb3Qtbm9kZT48c3ViLW5vZGUvPjwvcm9vdC1ub2RlPg==
<root-node><sub-node/></root-node>
Thanks to #luk2302 I was able to resolve the issue. Before decoding the string, I need to first encode it to Base64
byte[] dataBytes = Base64.getEncoder().encode(data.getBytes());
dataBytes = Base64.getDecoder().decode(dataBytes);
i have a String longblobimage, that is taken from the mysql database how do i convert it to a base64 String, and how do i know if its already an encoded image or not?
here is my attempt:
public String checkForEncode(String string) {
String regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
if(string.matches(regex)){
System.out.println("ERROR33:it's a Base64");
return string;
}
String encodedImage = Base64.encodeToString(string.getBytes(), Base64.DEFAULT);
return encodedImage;
}
so far this doesnt work to me for some reason
What are you importing to get access to
Base64.encodeToString(string.getBytes(), Base64.DEFAULT)
I'm trying to figure out how this is working, but the Base64 doesn't even have a documented static method that takes an encoding type.
You can translate a string to a base64 byteArray, which is how you would probably store the image to a file.
A String in Java is UTF-16 encoded, so storing a Base64 encoded byteArray back into a String doesn't make sense as that changes the encoding again.
byte[] encoded = Base64.encodeBase64(string.getBytes());
I have a string like that "QQBkADEBbgAxAXoA" I am creating a byte array of the string and using this code to convert it to string in c#.
string value = new UnicodeEncoding()).GetString(array)
I need this UnicodeEncoding in java. Is there a class that can perform it in java?
C# class UnicodeEncoding encodes on UTF-16.
String value = new String(bytes, "UTF-16LE");
The code above worked for me, since c# was using little endian representation, and Java UTF-16 is big endian.
The C# class UnicodeEncoding encodes the string using the UTF-16 encoding.
In Java you should be able to convert the bytes back to a string like this:
byte[] bytes = ...;
String value = new String(bytes, "UTF-16");
Or the other way around, convert a Java string to bytes using UTF-16 encoding:
byte[] bytes = value.getBytes("UTF-16");
How can I convert this byte[] to String :
byte[] mytest = new byte[] {100,25,28,-122,-26,94,-3,-26};
i get this : "d��^�" when I use :
new String( mytest , "UTF-8" )
Here is code java for creation of key :
m_key = new javax.crypto.spec.SecretKeySpec(new byte[] {100,25,28,-122,-26,94,-3,-26}, "DES");
Thanks.
In order to decode the byte array into something like ASCII, you need to know its original encoding. Otherwise you would need to treat it as binary.
Note: Base64 is intended for transferring binary data across networks.
I would suggest Base64 encoding your byte array. Then in your PHP code decoding the Base64 string back into a UTF-8 string.
In Java, here's how to Base64 encode your byte array and then decode it back to UTF-8:
import org.apache.commons.codec.binary.Base64;
public class MyTest {
public static void main(String[] args) throws Exception {
byte[] byteArray = new byte[] {100,25,28,-122,-26,94,-3,-26};
System.out.println("To UTF-8 string: " + new String(byteArray, "UTF-8"));
byte[] base64 = Base64.encodeBase64(byteArray);
System.out.println("To Base64 string: " + new String(base64, "UTF-8"));
byte[] decoded = Base64.decodeBase64(base64);
System.out.println("Back to UTF-8 string: " + new String(decoded, "UTF-8"));
/* the decoded byte array is the same as the original byte array */
for (int i = 0; i < decoded.length; i++) {
assert byteArray[i] == decoded[i];
}
}
}
The output from the above code is:
To UTF-8 string: d��^�
To Base64 string: ZBkchuZe/eY=
Back to UTF-8 string: d��^�
So if you wanted to use the same binary data in your PHP code, cut and paste the Base64 string into your PHP code and decode it back to UTF-8. Something like this:
<?php
$str = 'ZBkchuZe/eY=';
$key = base64_decode($str);
echo $key;
?>
I don't code in PHP, but you should be able to decode Base64 using this method:
http://php.net/manual/en/function.base64-decode.php
The above code should echo back the original binary data as UTF-8 (albeit with funny characters). The point is that the funny-looking string in the $key variable is representing the same binary data you had in the Java byte array:
d��^�
You should be able to pass the $key variable into your PHP encryption method.
with the way you are doing it makes no sense imo. you are creating a new string with the byte[] as an argument. i dont think that function is suppose to parse. so what you end up with is a lot of junk. but a little bit of googling got me this: http://www.mkyong.com/java/how-do-convert-byte-array-to-string-in-java/
Would m_key.getEncoded() give you the desired result.
Javadocs - SecretKeySpec
If not, you have to identify the Key provider that was used for the encoding (which resulted in the byte array that you have now) and decode.