Hi. Could anyone help me converting this Java function to GO?
public long complementOfTo(String hex){
HexToBin hextobin = new HexToBin();
long coord = 0;
int a = Integer.parseInt(String.valueOf(hextobin.hexToBin(hex).charAt(0)));
switch (a){
case 0:
coord = Long.parseLong(hex,16);
break;
case 1:
long c = Long.parseLong(hex,16);
long r = c ^ (long)(Math.pow(2, Math.ceil(Math.log(c)/Math.log(2))) -1);
coord = Long.parseLong("-".concat(String.valueOf(r)));
break;
}
return coord;
}
Interesting line converting a number to a string and then back to a negative integer
The switch case takes the first character of the conversion of the hex to bin and converts it to a integer you could really just switch on the first character instead.
HexToBin() converts a hexadecimal number to a binary and if the first digit is a zero I guess it pads it to a certain width. How many digits?
I may be mistaken but the thing the function does is to convert a hexadecimal number in a string into a two’s complement number (most common representation of signed integers on computers)