C#
private static byte[] ConvertStringToByteArray(string inputString)
{
return Encoding.UTF8.GetBytes(inputString);
}
public byte CalculateChecksum(string inputString)
{
byte[] inputBytes = ConvertStringToByteArray(inputString);
uint checksum = 0x7F;
foreach (byte b in inputBytes)
{
checksum ^= b;
}
return (byte)checksum;
}
public void SendData(string inputString)
{
byte[] inputBytes = ConvertStringToByteArray(inputString);
byte checksum = CalculateChecksum(inputString);
List<byte> outputBytes = new List<byte>(inputBytes);
outputBytes.Add(checksum);
outputBytes.Add(0x0D);
byte[] outputData = outputBytes.ToArray();
}
Alles anzeigen