user1155299
user1155299

Reputation: 927

encoding unsigned int

I have the following code in java that I need to convert to c++ to be able to encode unsigned int.

public static int enc_uint32be( int i, byte[] dst, int di ) {
        dst[di++] = (byte)((i >> 24) & 0xFF);
        dst[di++] = (byte)((i >> 16) & 0xFF);
        dst[di++] = (byte)((i >> 8) & 0xFF);
        dst[di] = (byte)(i & 0xFF);
        return 4;
}

I am a newbie to java. I trust several experts on this forum know both languages - can someone help me with the translation.

Upvotes: 1

Views: 343

Answers (2)

Plexico
Plexico

Reputation: 131

Actually the code in Java is almost the same in C++ with small modifications:

  • byte is not a valid type in C++, the equivalent is unsigned char
  • The array parameter type must be reworked as @MarkWilkins mentionned.

Upvotes: 1

Mark Wilkins
Mark Wilkins

Reputation: 41252

It depends on the data type that you are planning on using, but if you just change:

 byte[] dst

to

unsigned char dst[]

in the function declaration, then it should behave the same (and change the byte usages in the function to unsigned char as well).

Upvotes: 3

Related Questions