Porting a scala function to C++

1

I'm trying to port the following scala code to c++

Scala code:


def getLong(bytes: Array[Byte]): Long = {
    //MessageDigest.getInstance("MD5")
    val hash = messageDigest.get().digest(bytes)

    val str = messageDigest.get().digest(bytes).map(_.toChar).mkString("")

    val bb = ByteBuffer.wrap(hash)
    bb.order(ByteOrder.LITTLE_ENDIAN)
    val number: Long = bb.getInt & 0xffffffffL 

    number
  }

C++ code:

unsigned long get_long(string key) {

    unsigned long n = key.length();

    unsigned char* uchrs = reinterpret_cast<unsigned char*>(const_cast<char*>(key.c_str()));
    unsigned char outbuf[MD5_DIGEST_LENGTH];
    MD5(uchrs, n, outbuf);

    unsigned long number = *((unsigned long*)outbuf);

    return number;

}

When I call the function with this input, its giving different output values, does anyone know what I'm doing wrong?

Example:

getLong("158750493953113325943f6e61350d949848bd2877f97aee4b6".getBytes) => 2384178603 

get_long("158750493953113325943f6e61350d949848bd2877f97aee4b6") => 15019855082967248299

I tried something like this in the c++ code:

int num_int = number  & INT_MAX & 0xffffffff;

it doesn't help (returning a different value 236694955).

c++
scala
asked on Stack Overflow Jul 9, 2019 by cppnoob

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0