c#压缩base64与java压缩base64对接,C#和Java之间的差异的Base64

image is the string of an image file .

I have code as follows in C#:

Convert.ToBase64String(image);

and code as follows in Java:

org.apache.commons.codec.binary.Base64.encodeBase64(image.getBytes())

The result is different.

Somebody says its because

Java byte : -128 to 127

C# byte : 0 to 255

But how can I fix this? How can I implement C#'s Convert.ToBase64String() in Java?

I need the same result as in C# by using Java.

解决方案

You're base64 encoding a string? What do you want that to do? You first need to convert the string to a sequence of bytes, choosing an encoding such as UTF-8 or UTF-16.

My guess is that you managed to use different encodings on both sides. Java's String.GetBytes() uses the default charset (Probably something like Latin1 on western windows versions). For C# you didn't post the relevant code.

To fix this, choose an encoding and use it explicitly on both sides. I recommend using UTF-8.

On the Java side you should use the correct method for encoding, so you don't end up with "modified UTF-8", but since I'm not a java programmer, I don't know which methods output modified UTF-8. I think it only happens if you abuse some internal serialization method.

signed vs. unsigned bytes should not be relevant here. The intermediate byte buffer will be different, but the original string, and the base64 string should be identical on both sides.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值