BASE64编码 android 与 C# 结果不一致

JAVA代码:

    ByteArrayOutputStream v0 = new ByteArrayOutputStream();
        arg3.compress(Bitmap$CompressFormat.PNG, 100, ((OutputStream)v0));
        return Base64.encodeToString(v0.toByteArray(), 0);

C#代码:

 using (var ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                byte[] imageBytes = ms.ToArray();
                var str = Convert.ToBase64String(imageBytes, Base64FormattingOptions.InsertLineBreaks);
                return str;
            }

对同一张png图片进行编码后。得到的结果不一致。
开头和结尾是一样的。中间部分不一样。结果的长度也不一样。相差甚远。

Java 中 byte : -128~127

C# 中 byte : 0~255

因此虽然表达到通信层上都会根据通信适配去转换,但是由于base64是提前就编码了的所以会造成不一致问题。

但是只要能读取出显示图片就行了,不必太纠结过程,可参考:
http://blog.csdn.net/a9529lty/article/details/9417943

Java 中 byte : -128~127

C# 中 byte : 0~255.
从github上翻出来一段代码,正好是将andorid 的base64 类进行完整翻译得到的C#类。
使用后结果还是一样。与JAVA的编码后结果存在差异。
后来考虑是JAVA和C#的byte差异。但是没找到很好的解决方法去解决这个问题。

详细参考,希望对你有帮助http://blog.csdn.net/tomatozq/article/details/20773559

/**
* BASE标准解码
*
* @author lz
* @param buff
* @return
*/
public static String decodeBase64(String str) {
return str == null ? null : StringUtils.newStringUtf8(base64.decode(str));
}

/**
 * BASE64 编码
 * 
 * @param s
 * @return
 */
public static String encodeBufferBase64(byte[] buff) {
    return buff == null ? null : encoder.encodeBuffer(buff).trim();
}

/**
* BASE标准解码
*
* @author lz
* @param buff
* @return
*/
public static String decodeBase64(String str) {
return str == null ? null : StringUtils.newStringUtf8(base64.decode(str));
}

/**
 * BASE64 编码
 * 
 * @param s
 * @return
 */
public static String encodeBufferBase64(byte[] buff) {
    return buff == null ? null : encoder.encodeBuffer(buff).trim();
}

解码 能解出图片 编码 就是正确的