[英]Issue with Encoding base64 in PHP and Decoding base64 in Java
字符串 - 使用base64在PHP中编码的“gACA”。 现在我正在尝试使用base64在java中解码。 但解码后得到荒谬的价值。 我试过这样的:
public class DecodeString{
{
public static void main(String args[]){
String strEncode = "gACA"; //gACA is encoded string in PHP
byte byteEncode[] = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(strEncode );
System.out.println("Decoded String" + new String(k, "UTF-8"));
}
}
输出: ??
请帮帮我
试试这个,它对我来说很好(但我解码文件):
Base64.decodeBase64(IOUtils.toByteArray(strEncode));
所以它看起来像这样:
public class DecodeString{
{
public static void main(String args[]){
String strEncode = "gACA"; //gACA is encoded string in PHP
byte[] byteEncode = Base64.decodeBase64(IOUtils.toByteArray(strEncode));
System.out.println("Decoded String" + new String(k, "UTF-8));
}
}
请注意,您将需要额外的库:
Java内置Base64编码器 - 解码器,无需额外的库来解码它:
byte[] data = javax.xml.bind.DatatypeConverter.parseBase64Binary("gACA");
for (byte b : data)
System.out.printf("%02x ", b);
输出:
80 00 80
它是带有十六进制代码的3个字节: 80 00 80
public static void main(String args[]) {
String strEncode = "gACA"; //gACA is encoded string in PHP
byte byteEncode[] = Base64.decode(strEncode);
String result = new String(byteEncode, "UTF-8");
char[] resultChar = result.toCharArray();
for(int i =0; i < resultChar.length; i++)
{
System.out.println((int)resultChar[i]);
}
System.out.println("Decoded String: " + result);
}
我怀疑这是一个编码问题。 在C#文本文件中发出大约65533 reading阅读这篇文章表明第一个和最后一个字符是\\“。 在中间有一个char 0.您的结果可能是“”或“0”,但编码错误。
首先,你使用的代码不应该编译,它在"UTF-8
之后缺少一个结束语。
是的, "gACA"
是格式的有效base64
字符串,但它不会解码为任何有意义的UTF-8文本。 我想你使用了错误的编码,或者以某种方式弄乱了字符串......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.