1

I'm not experienced in Java and so this is going straight over my head:-

Java Code:

long foo = 1234567890;
byte[] boo = ByteBuffer.allocate(8).putLong(foo).array();

C# Code:

long foo = 1234567890;
byte[] bar = BitConverter.GetBytes(foo);

// reverse to match Java's Big Endianess
byte[] boo = bar.Reverse().ToArray();

In the Java sample, boo = 0, 0, 0, 0, 73, -106, 2, -46

However in C#, boo = 0, 0, 0, 0, 73, 150, 2, 210

Can someone with a bigger brain, explain why these differ?

Many thanks!

1 Answer 1

12

Java is using signed bytes, C# is using unsigned. Note that all values < 127 match, and values > 128 are converted to a negative number. If you converted the C# array from byte to sbyte, the values would match.

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

3 Comments

Exactly. The corresponding data type in C# is sbyte (signed byte).
More specific: for values > 127 the Java value is the C# value subtracted by 256.
Shouldn't have deleted my answer :) This is what I wrote. Have an upboat!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.