Pokazywanie postów oznaczonych etykietą Byte64. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Byte64. Pokaż wszystkie posty

sobota, 11 maja 2013

JAVA vs .NET differences in practice

Hello long time I didn't write any post - it's time to change it! Today I would like to show you in practice some differences between .NET and Java. I mean in some primitives data types and other. Few days ago I was involved to some task with hashed users passwords. It was necessary for test Web Services (JAX-WS) whose connect with some other services using hashed password. We have some hash algorithm which was written in .NET and we would like to have the same result in Java but it wasn't working. OK let's have some details. This is some similar example in .NET


From .NET point of view everything is fine, but from Java is not! Why? - In .NET byte range is between 0-255 in Java is between (-127,128). This is 1 fail which I have discovered in code. Solution? - change the type from byte to int in Java and 1 problem fixed up.(But not really...). OK in Java there is a bitwise operator ^ (XOR) so - Convert.ToByte in Java is simple.
But XOR wouldn't work on int type, if I cast int to byte i will lost precision. Damn! There is another valuable information and comparison -Convert.ToBase64String and SHA512 algorithm. What is a problem with SHA512 algorithm in Java? There is to many code (in pure JDK) to write, but if I add just 1 library/jar Apache Commons Codec my code reduces from around 20 lines of code just to 1. But what about ToBase64String? I was searching in google and there was to many code to write the equivalent. In .NET the programmer sent to the method ToBase64String a whole table with bytes, in Java even when I tried get all bytes and "stick" them together using StringBuffer/StringBuilder or many various combinations and approaches and send it to equivalent Base64 method from Apache Commons Codec but without good effects. Time was running up, and I feel some solution in the air, just like a wolf which felt the blood. I didn't give up, what I did is just use online .NET/C# compilator and write the code and run it!

So my final solution in Java was really short and works well. After this I gained a lot in my new Team mates eyes. But they don't know that what kind a "lazy" bustard I am ;)

What is conclusion ? -Sometimes we are wasting time to find solution using another libraries, searching equivalent type in Java/.NET and a solution is just at your fingertips. Sometimes it's better to run any online compiler and run the code and see the result, save the output and use it in our code without wasting time to translate byte> 128 to int and many miracles in Java/.NET