2

Possible Duplicate:
Converting 32-bit unsigned integer (big endian) to long and back

I want to translate this expression in Java

char tab[100];
tab[10] = '\xc0';

tab[48] = '\x80';

uint32_t w = 0x67452301;

uint32_t x = 0xefcdab89;

uint32_t y = 0x98badcfe;

uint32_t z = 0x10325476;

a = ((b & c) | (~b & d)) + (*(uint32_t*)(tab+0x00)) + a - 0x28955B88;

a = ((a << 0x07) | (a >> 0x19)) + b;

I'have tried this but...

char[] tab = new char[64];

 tab[10] = (char) 0xc0; 
 tab[48] = (char) 0x80; 

but the value is not the right one, is there another way to assign \0x80 in a char[] ? How can i interprete this kind of cast in java ((uint32_t)) ?

Many thanks !

Community
  • 1
  • 1
user1568522
  • 95
  • 1
  • 2
  • 3

1 Answers1

6

Type int is 4 bytes, i.e. 32 bits in java. So the regular 32bits int from C may be translated as regular int in java.

Unsigned int is not supported by java language, so use long that contains 8 bytes to represent unsigned 32 bit variables from C.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • "long that contains 8 bytes"? Am I understanding correctly? Long is 64 bits. – Sridhar Sarnobat Jan 31 '14 at 00:59
  • I didn't get it at first but after reading that article it made sense. http://jessicarbrown.com/resources/unsignedtojava.html also the article uses "& 0xffffffffL" in order to translate to long properly. – Sauleil Jun 20 '14 at 14:46
  • int is 32 bit and long is 64 bit same in c uint32_t in 32 bit how long and uint32_t will relate each other? because in final result if i have total 8 byte of data then it will increase to 16 byte. – Parth Aug 14 '17 at 07:19
  • FYI the URL for the unsigned to java article has changed. the new url is: http://www.raebear.net/programming/java/unsigned-numbers/ – Jessica Brown Jan 03 '18 at 19:43