0

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

Stephan
  • 1,639
  • 3
  • 15
  • 26

1 Answers1

2

ByteBuffer in Java keeps data in Big-Endian order. BitConverter depends on computer-architecture, and typically is Little-Endian (the right way is to check BitConverter.IsLittleEndian property).

So to get in C# output same to Java - you have to inverse order of bytes:

long value = BitConverter.ToInt64(byteContents.Reverse().ToArray(), 0);

More correct way would be to check BitConverter.IsLittleEndian to do the task:

long value =
    BitConverter.ToInt64(
        BitConverter.IsLittleEndian
            ? byteContents.Reverse().ToArray()
            : byteContents,
        0);
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
  • I have a follow-up question: is the generated value unique for each string (is it a perfect hash function) or can two strings have the same long value? – Stephan Jul 16 '20 at 10:16
  • 1
    If you get string longer than 8 bytes - then you will have a problem, since you have to truncate it somehow. Until then - it is ok. – Ulugbek Umirov Jul 16 '20 at 10:19