I am converting a C++ program to Java and got completely stuck in the following method which blew my mind. Would you be kind enough to explain what this method is doing?
long TSBCA::GetSignedValue(const NDataString &value)
{
static NDataString s;
s = value;
long multiplier(1);
size_t len(s.Len());
if (len != 0)
{
if (s[0] >= (char)0xB0 && s[0] <= (char)0xB9)
{
s[0] &= 0x7F; //Bit Pattern: 0111 1111
multiplier = -1;
}
else if (s[len - 1] >= (char)0xB0 && s[len - 1] <= (char)0xB9)
{
s[len - 1] &= 0x7F; //Bit Pattern: 0111 1111
multiplier = -1;
}
else
multiplier = 1;
}
else
multiplier = 1;
return s.ToLong() * multiplier;
}
EDIT:
My initial Java version:
private long getSignedValue(final String value){
byte[] bytes = value.getBytes();
int length = bytes.length;
long multiplier = 1L;
if (bytes.length > 0){
if (bytes[0] >= (char)0xB0 && bytes[0] <= (char)0xB9){
bytes[0] &= 0x7F; //Bit Pattern: 0111 1111
multiplier = -1;
}
else if (bytes[length - 1] >= (char)0xB0 && bytes[length - 1] <= (char)0xB9)
{
bytes[length - 1] &= 0x7F; //Bit Pattern: 0111 1111
multiplier = -1;
}
else
multiplier = 1;
}
else
multiplier = 1;
return Long.parseLong(Arrays.toString(bytes))* multiplier;
}
Did I do it right?