C#和Java之间的差异的Base64 [英] Base64 difference between C# and Java

查看:1781
本文介绍了C#和Java之间的差异的Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片是图像文件的字符串。

image is the string of an image file .

我有code在C#如下:

I have code as follows in C#:

Convert.ToBase64String(图片);

和code作为在Java中如下:
org.apache.commons。codec.binary.Base64.en codeBase64(image.getBytes())

and code as follows in Java: org.apache.commons.codec.binary.Base64.encodeBase64(image.getBytes())

的结果是不同的

有人说,它是因为


  • Java字节:-128〜127

  • Java byte : -128 to 127

C#字节:0至255

C# byte : 0 to 255

但是,如何才能解决这个问题?我如何能实现C#的 Convert.ToBase64String()在Java中?
我通过使用Java需要相同的结果在C#。

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.

推荐答案

你的base64编码的字符串的?你要什么,要怎么办?首先,您需要将字符串转换为字节序列,选择一个编码,如UTF-8或UTF-16。

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.

我的猜测是,你成功地在两侧使用不同的编码。 Java的 String.GetBytes()使用默认字符集(大概就像Latin1的西方Windows版本)。对于C#你没有张贴相关code。

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.

要解决这个问题,选择一个编码并明确使用它的两侧。我建议使用 UTF-8

在Java端,你应该使用的编码正确的方法,这样你就不会用修订的UTF-8结束了,但因为我不是一个Java程序员,我不知道方法输出修订的UTF-8 。我的认为的它只有在你滥用一些内部序列化方法发生了。

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.

签署对无符号字节不应该与此有关。中间字节的缓冲区会有所不同,但原来的字符串,以base64字符串应该是双方一致的。

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.

这篇关于C#和Java之间的差异的Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆