我正在将 C++ 程序转换为 Java,并完全陷入了以下让我大吃一惊的方法。您能解释一下这种方法的作用吗?
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;
}
编辑:
我的初始 Java 版本:
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;
}
我做对了吗?