I am trying to convert a small piece of code to Java from C#. [I think don't have to say I'm a noob. :P]
The two codes below returns differently I don't understand why. Thanks for any help.
P.S.: I've checked the question here but the answer doesn't fix my problem.
C#.NET
public class Test
{
private static sbyte[] HexToByte(string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
sbyte[] arr = new sbyte[hex.Length >> 1];
int l = hex.Length;
for (int i = 0; i < (l >> 1); ++i)
{
arr[i] = (sbyte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
private static int GetHexVal(char hex)
{
int val = (int)hex;
return val - (val < 58 ? 48 : 55);
}
public static void Main()
{
Console.WriteLine(HexToByte("EE")[0]);
}
}
OUTPUT: -18
JAVA
class Test
{
private static int GetHexVal(char hex)
{
int val = (int)hex;
return val - (val < 58 ? 48 : 55);
}
private static byte[] HexToByte(String hex) throws Exception {
if (hex.length() % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.length() >> 1];
int l = hex.length();
for (int i = 0; i < (l >> 1); ++i)
{
arr[i] = (byte)((GetHexVal((char)(hex.charAt(i << 1) << 4)) + (GetHexVal(hex.charAt((i << 1) + 1)))));
}
return arr;
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println((HexToByte("EE")[0]));
}
}
OUTPUT: 39
I don't understand why this happens and what is the way to overcome it?