Blog Stats
  • Posts - 45
  • Articles - 0
  • Comments - 16
  • Trackbacks - 20

 

.net byte v/s java byte

So a byte is a byte is a byte unless it is not! That in one sentence sums up my findings from yesterday. A byte in .NET represents a unsigned byte (0 - 255) automatically but a byte in Java is a signed byte(-128 - 127) and no there is no way to get an unsigned byte. I never really came across this problem till I was running some encryption algorithms on both. I need to encrypt a string in .NET and decrypt it on Java. For this I was converting the byte array into a hex string and asking users to enter it. However, because of the difference in interpretation of bytes I am running into problems.
NOTE: There is a signed byte in .NET available called ssbyte. I am not sure how an encryption algorithm can stuff it's data into it thgh. Sad! Sad! Sad!
CORRECTION: Okay so I had a brain-dead day and worte this out. It does not really matter how the two languages interpret the bits as long as the bits are the same going in to the encryption/decryption libraries.I am keeping this here to remind myself how stupid I can be.

Feedback

# re: .net byte v/s java byte

Gravatar I am a bit lost here ...

can you put up the "differring" hex? because whether it is signed or not the hex for the 8 bits is going to be the same (its just if a value is over 128 a signed implementation in reading it will assume that it is negative)

As for your algorithm putting data into ssbytes ... umm it would be remarkably easy ... run your algorithm on a normal byte array. Then instantiate a ssbyte array of the same size ... ssbyte[i] = byte[i] - 128;

Either way at some point java surely has a type that is 0..255
1/27/2006 7:43 PM | Greg Young

# re: .net byte v/s java byte

Gravatar hi this is rajeev this is very good tht we can easily know the differences between the .net and java 2/4/2007 2:45 AM | rajeev

# re: .net byte v/s java byte

Gravatar Thanks for the tip! I googled for info about Java bytes and found this very helpful! 9/10/2008 1:13 PM | Zack

Post a comment





 

 

 

Copyright © Rishi Pande