I am trying to convert a string into a numerical value in c# and java, with the condition that the conversion must be equal. The following Java - Test runs:
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class JavaTest {
@Test
public void TestJava(){
byte[] bytes = "ABCDEFGH".getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);
long value = buffer.getLong(0);
assertThat(value, is(4702394921427289928L));
}
}
When I try to convert the same value in c# with the BitConverter
[Test]
public void TestCSharp()
{
byte[] byteContents = Encoding.UTF8.GetBytes("ABCDEFGH");
long value = BitConverter.ToInt64(byteContents);
Assert.AreEqual(value, 4702394921427289928L);
}
the test is red.
Is there any way this can work? Is there an alternative?
EDIT: fyi: both byte-arrays are equal so the error must be in the conversion from byte[] to long