2

我想计算通过蓝牙发送的消息的 LRC。这是例如一条消息:

(消息结构为 STX MESSAGE FS EXT LRC)

02 1212004123422826E0B8C0F000F0A00182620000THYRON SYSTEMS沃特​​福德UKR 1C 03 60

STX = 02
MESSAGE = 1212004123422826E0B8C0F000F0A00182620000THYRON SYSTEMS WATFORD UKR
FS = 1C
EXT = 03
LRC = 60

我要做的是通过对通信消息中的每个字符(不包括 STX 字符但包括 EXT characterr)执行模 2 二进制和来计算 LRC。

在计算 LRC 之前,我必须将此 NSString 转换为 HexaString :

31323132 30303431 32333432 32383236 45304238 43304630 30304630 41303031 38323632 30303030 54485952 4f4e2053 59535445 4d532057 4154464f 52442055 4b521c03

使用方法:

- (NSString *) stringToHex:(NSString *)str
{   
    NSUInteger len = [str length];
    unichar *chars = malloc(len * sizeof(unichar));
    [str getCharacters:chars];

    NSMutableString *hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ )
    {
        [hexString appendFormat:@"%02x", chars[i]];
    }
    free(chars);

    return [hexString autorelease];
}

然后我必须将它转换为字节数组。使用方法:

- (NSData*) hexToBytes:(NSString *) hexaStr {

    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= hexaStr.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString * hexStrTmp = [hexaStr substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStrTmp];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [data appendBytes:&intValue length:1];
    }
    return data;
}

然后我尝试用以下内容计算我的 LRC,应该是 60:

- (void) calculateLRC:(NSString *) text {

    NSData * data = [self hexToBytes:text];

    NSUInteger size = [data length] / sizeof(const char);
    const char * array = (const char*) [data bytes];

    char checksum = 0;

    for( uint32_t i = 0 ; i < size; i++) {

        checksum += * array++;
    }

    NSLog(@"Checksum = %c", checksum);

    self.LRC_Check = checksum;
}

问题是“校验和”中没有值“60”。有人可以帮我吗?

提前谢谢!

4

2 回答 2

1

我有答案!

- (void) calculateLRC:(NSString *) text我必须替换的方法中:

checksum += * array++;

和 :

checksum ^= * array++;

我认为模数字符是目标 C 中的“%”...

于 2012-09-21T12:17:35.437 回答
0

我需要迅速做到这一点

这似乎有效。.需要做一些严格的测试虽然..

var str = computeLRC(hexString:"373203")
print(str)

// 返回 06

func computeLRC (hexString: String ) -> String {
    var checksum : UInt16 = 0
    var my = ""
    for i in stride(from: 0, to: hexString.count, by: 2) {
        
        let indexStartOfText = hexString.index( hexString.startIndex, offsetBy: i)
        let indexEndOfText = hexString.index( indexStartOfText, offsetBy: 2)
        let substring3 = hexString[indexStartOfText..<indexEndOfText]
        
        let intResult =  Int(String(substring3) , radix: 16)
        guard let myUnicodeScalar = UnicodeScalar(intResult!) else {
            return ""
        }
        // convert UnicodeScalar to Character
        let myCharacter = Character(myUnicodeScalar)
        
        my += String(myCharacter)
    }
    
    for myChar in my {
        var byte: UInt16 = Array(String(myChar).utf16)[0]
        checksum ^= byte
    }
    
    return String(format:"%02X", checksum)
    
}
于 2019-04-18T14:37:14.537 回答