Char bitwise inversion calculation

I am trying to cancel the development of a serial port device using hdlc for its packet format. Based on the documentation, the package should contain a bitwise reference to the command (the first 4 bytes), which in this case "HELO", Monitoring the serial port using the source program shows what the bitwise inversion should be:

 HELO -> b7 ba b3 b0
 READ -> ad ba be bb

The problem is that I do not get the value even remotely.

public object checksum
    {

        get
        {
            var cmdDec = (int)Char.GetNumericValue((char)this.cmd);
            return (cmdDec ^ 0xffffffff);
        }
    }
+4
source share
3 answers

You need to work with bytes, not characters:

string source = "HELO";

// Encoding.ASCII: I assume that the command line has ASCII encoded commands only  
byte[] result = Encoding.ASCII
  .GetBytes(source)
  .Select(b => unchecked((byte)~b)) // unchecked: ~b returns int; can exceed byte.MaxValue
  .ToArray();

Test (let it be designated resultas hexadecimal)

// b7 ba b3 b0
Console.Write(string.Join(" ", result.Select(b => b.ToString("x2")))); 
+2
source

Char . .


, this.cmd - ? BitConverter.ToUInt32()

PSEUDO: ( )

public uint checksum
{
    get
    {
        var cmdDec = BitConverter.ToUInt32(this.cmd, 0);
        return (cmdDec ^ 0xffffffff);
    }
}

if this.cmd - , Encoding.UTF8.GetBytes(string)

+1

, , . , :

int i = 5;

var j = i ^ 0xFFFFFFFF;
var k = ~i;

, , XOR- . # Bitwise-NOT ~.

j long, 4294967290, k int, -6. , j 32 0, . , , , , .

0
source

Source: https://habr.com/ru/post/1666224/


All Articles