   [java] c++ data types in Java
  Hi!Well, I'm porting some algorithms form c++ to java and I would reallyappreciate if someone could help me out with a couple of questions.First off I changed the primitive types from c++ to java:    c++   Javaunsigned char->charlong    ->intunsigned long->longUnfortunately that only works in cases where no overflow occures, as in my algorithms:unsigned long val1 = ...unsigned long val2 = ...val1 ^= val1; //probabilly will be overflow since the values usually are very highIf I translate the data types above to java... I will NOT get the overflowthat is supposed to happen in many cases :(So, does anyone know of a sollution for this?Thx!   
  I'm not too sure precisely what you are trying to do, or why you would want an overflow to happen, but bear in mind that a char in C is not the same as a char in java. A char in C is normally 8-bits or one byte, a char in java is 16-bits or two bytes. If you want an equivalent to char in java, use the 'byte' type instead which is only 8-bits.   ;  Yeh, thx... but there is no problem in the case of the char conversion,because of the way they are used in the application...Anyway, why I want the overflow to behave correctly is that those areadvanced encryption algorithms that use the overflow mechanism.(I would rather not put the algorithms into a dll, since I later compilethe java application into an executable)So an overflow must behave correctly :)And the java long will not overflow at the same value as the c++ unsigned long :(thx anyway   ;  Besides the char info you for got above, remember in Java int is 32 bits wide, and long is 64 bits wide. Maybe 'long' is too long for what you want.Unsigned long wouldn't be a close match for Java's long.;  last ap, yes exactly that is the problem... since there will be nooverflow that I wanted :)But anyhow... now that I'm sitting her implementing the code it seemslike my long variables I use are the same as int ??Seems like I can't make java use 64bits on the long??Oh... no problem.... seems like I need to append "L" in the endof a variable...   ;  a ^= b could never do an overflow. It's a bitwise xor operation.Advanced encryption algorithms? You should try to understand them before porting, and look if ther is no public domain or BSD licence code already.   ;  Oh k, thx!That was my missunderstanding on that operator, I thought it was "power" :)anyway, it's a vairant of a blowfish encryption algorithm.an example of what I meant:c++ version:unsigned long key = 0x890BD109L;unsigned char* c = &ke;unsigned long k = c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3];java version:long key = 0x890BD109L;byte[] c = getULongBytes(key);unsigned long k = c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3];When I run both versions... I don't get the same values on thek variable.And that is because thec[x] << yreturn different values in both versionsAnd yes... I have not much knowledge of the blowfish encryptionand neither would I like to spend time on learning it, sinceI have other stuff I would rather be coding - yes, I just wantto get past this :)Thx!;  Since you don't want to spend time properly implementing it, why not use the default SunJCE, which includes an implementation of Blowfish (afaik)?Edit: Check out this article which shows how to use SunJCE and Blowfish.Edit2: I haven't actually used it myself so am not aware of what current issues there are with it.   ;  Well, I would be more than happy to use a working implementationof the blowfish algorithm ;)But unfortunately this is a special implementation, and thus Ineed to translate it to java.;  All primitives in Java are signed. You have to take care when you so that the signed bit are handled correctly.You wrote:java version:long key = 0x890BD109L;byte[] c = getULongBytes(key);unsigned long k = c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3];Here "c" can contain 8 bit of information including the signed bit. Used with the or operator the byte is extended to an int so that the int contains the same value as the signed byte. If c[3] contains 0xff (unsigned char) or -1 (byte) after the conversion it is evaluated as 0xffffffff (or -1 int).What you have to do is mask out only the part that you need. In other words, whenever using a byte you have to & it with 0x000000ff, like this:long k = (c[0] & 0xff) << 24 | (c[1] & 0xff) << 16 | (c[2] & 0xff) << 8 | (c[3] & 0xff);   