4

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.

3
  • 1
    You're saying image is a string. But Convert.ToBase64String doesn't take a string, only a byte[]. Commented Dec 15, 2011 at 10:24
  • 1
    Can you show aus how you get the image data into the strings? In both languages please. Commented Dec 15, 2011 at 10:33
  • Have the person that explained the reason, explain the reason your code is wrong, otherwise you need to provide the C# code and provide clarification. Commented Dec 15, 2011 at 12:17

3 Answers 3

6

First you need to realise that a byte stores 256 values whether its signed or unsigned. If you want to get unsigned values from a signed byte (which is what Java supports) you can use & 0xFF

e.g.

byte[] bytes = { 0, 127, -128, -1}; for(byte b: bytes) { int unsigned = b & 0xFF; System.out.println(unsigned); } 

prints

0 127 128 255 

The simple answer is you don't need a byte[] which has the same values. ;)

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

you are right.it is the encoding.i read the image file into string.after i read the image file to byte[] in ascii,the result is equal now.thanks
0

I also encountered the same problem. There is a saying on the Internet:

Java byte : -128 to 127 | C# byte : 0 to 255 

I looked up the algorithmic principle of java base64 encoding and decoding. Use C# to implement the base64 algorithm and run the program: the result is the same as

Convert.ToBase64String(byteArray). 

Finally found that the best way to solve this problem is:

Uri.EscapeDataString(Convert.ToBase64String(byteArray)). 

It should be noted that this is the reason for the special characters in the URL.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.