Convert C ++ bitmap to Java

I am converting a C ++ program to Java and am completely stuck in the following method that blew my mind. Could you explain what this method does?

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 version of 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;
}

Did I do it right?

+5
source share
3 answers

It takes a byte string (i.e. not text) and converts a long one into it. It relies on many specific things related to the implementation, and looks broken: it extracts a bit of a sign from two different places. Another problem is an unnecessary non-reinstallation (caused by a static variable).

+1
source
s[0] &= 0x7F;

- s[0] 7F , , . s[len-1], :

  • , (0x30 - 0x39 == '0' - '9' 0xB0 - 0xB9 - 0x80.)
  • ,

Edit

:

  • , JUnit , , , .
  • byte ( )
  • new String(byte[]) , Arrays.

:

// Bit Pattern: 0111 1111
private static final int BYTE_7F = 0x7F;

// '0' with sign bit set
private static final byte BYTE_NEGATIVE_0 = (byte) 0xB0;

// '9' with sign bit set
private static final byte BYTE_NEGATIVE_9 = (byte) 0xB9;


private long getSignedValue(String value) {

    byte[] bytes = value.getBytes();
    final int length = bytes.length;
    long multiplier = 1;

    if (0 < length) {
        if (bytes[0] >= BYTE_NEGATIVE_0 && bytes[0] <= BYTE_NEGATIVE_9) {

            bytes[0] &= BYTE_7F; 
            multiplier = -1;

        } else if (bytes[length - 1] >= BYTE_NEGATIVE_0 && bytes[length - 1] <= BYTE_NEGATIVE_9) {
            bytes[length - 1] &= BYTE_7F;
            multiplier = -1;
        }
    }

    return Long.parseLong(new String(bytes)) * multiplier;
}

, .

+1

, - ( ). ( ) 0xB0 0xB9, , ( 0x30 0x39, '0' '9'). , .

0

All Articles