0

我有一个关于使用目标 C 解析字符串的快速问题。目前,我有一个输出 *A_1_2_3_4_~ 的格式。此字符串具有消息开头 (*)、标头 (A)、值 (1) 和消息结尾 (~)。我已经为 arduino 编写了这段代码,但似乎无法让它在目标 c 中正常工作。该函数的作用是将每个值分开并对其进行分类。非常高效和坚固。如果我发送 *A_1_1_1_~,它会识别出 A 代表某物,值为 1、1、1。无论如何,这里的代码:

//protocol for parsing
-(void)protocol:(NSString*)input type:(Boolean*)comType
{
    Byte place = 0, index, ID, average, setGet; //any value < 256
    int probeType, tempType;
    char Header;
    NSString *output; //used for parsing
    double probeValue, tempValue;

//    if(comType){}
//    //wire = ""; //reset wire
//    else
//        analog.text = @"";//serial = ""; //reset serial

    //if error in message example: A_1_1_A_1_A_A_A*A_1_1_8.27_1_15.67_3_~
    //use '*' as start of message character and grab from *A_1_1_8.27_1_15.67_3_~

    probeType = input.length; //get length of string

    index = [input rangeOfString:@"*" options:NSBackwardsSearch].location; //index = input.lastIndexOf("*"); //index contains start of message
    if(index < 0)  //not available or incorrect format
        return;
    input = [input substringFromIndex:index]; //remove incorrect stuff
    average = [input rangeOfString:@"~"].location; //input.indexOf("~", index); //grab end of message
    input = [input substringWithRange:NSMakeRange(index+1, average+index)]; //input = input.substring(index + 1, average + 1); //grab valid string only, lower bandwidth
    //    [self alert: input];
    for(Byte i = 0; i < 8; i++) //limit the message length as well
    {
        //        index = [input rangeOfString:@"_"].location; //grab location of
        index = [input rangeOfString:@"_" options:Nil range:NSMakeRange(place, input.length)].location;//index = input.indexOf("_", place); //gets first update
        //        [self alert:index];
        if(index < 0 || [input characterAtIndex:index+1] == '~')//input.substring(place, index + 1) == "~") //no '_' found or EOM
            break; //two birds one stone
        output = [input substringWithRange:NSMakeRange(place, index)];//output = input.substring(place, index); //grab in between
        //input = [input stringByReplacingCharactersInRange:NSMakeRange(0, index) withString:@""];//remove already read

        switch(i)
        {
            case 0: //header
                Header = [output characterAtIndex:0]; //fast conversion from String to char
                break;
            case 1: //ID
                ID = [output intValue]; //toInt() returns long...not int; perfect for conductivity
                break;
            case 2: //Probe Type
                probeType = [output intValue];
                break;
            case 3: //Probe Value
                probeValue = [output doubleValue];
                break;
            case 4: //Temperature Type
                tempType = [output intValue];
                break;
            case 5: //Temperature Value
                tempValue = [output doubleValue];
                break;
            case 6: //Average
                average = [output intValue];
                break;
            case 7:
                setGet = [output intValue];
                break;
        }
        place = index + 1;
        [self alert: output];
    }
}
4

2 回答 2

1

目前尚不清楚您的所有参数对于格式的外观是什么,但我会这样做以从您的字符串中提取数字。像您的问题一样,我在字符串中查找第一个“*”,从末尾向后搜索。

 - (void)viewDidLoad {

    [super viewDidLoad];
    NSCharacterSet *nonNumbers = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *s = @"A_1_1_A_1_A_A_A*A_1_1_8.27_1_15.67_3_~"; // test string
    NSInteger locationOfAsterisk = [s rangeOfString:@"*" options:NSBackwardsSearch].location;
    if (locationOfAsterisk != NSNotFound) {
        NSString *validString = [s substringFromIndex:locationOfAsterisk + 1];
        NSMutableArray *parsedArray = [[s componentsSeparatedByString:@"_"] mutableCopy];
        NSIndexSet *indxs = [parsedArray indexesOfObjectsPassingTest:^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
            return [obj rangeOfCharacterFromSet:nonNumbers].location != NSNotFound ;
        }];
        [parsedArray removeObjectsAtIndexes:indxs];
        [parsedArray insertObject:[validString substringToIndex:1] atIndex:0];
        NSLog(@"%@",parsedArray);

    }else{
        NSLog(@"Not a valid format");
    }
}

这将返回一个数组,其中第一个元素是星号后面的字母,后面是波浪号之前的所有数字。

于 2013-07-19T01:36:47.790 回答
0
//protocol for parsing
-(void)protocol:(NSString*)inputString type:(Boolean*)comType
{
    int probeType, tempType, index, ID, average, setGet;
    NSMutableString *Header;
//    NSString *output; //used for parsing
    double probeValue, tempValue;

    //if error in message example: A_1_1_A_1_A_A_A*A_1_1_8.27_1_15.67_3_~
    //use '*' as start of message character and grab from *A_1_1_8.27_1_15.67_3_~

    probeType = inputString.length; //steal probeType for now, probeType contains length of string

    index = [inputString rangeOfString:@"*" options:NSBackwardsSearch].location; //index contains SoM, index
    if(index < 0)  //not available
        return;
    inputString = [inputString substringFromIndex:index]; //remove incorrect stuff before SoM
    average = [inputString rangeOfString:@"~"].location; //grab end of message
    inputString = [inputString substringWithRange:NSMakeRange(index+1, average)]; //grab valid string from SoM with length
    inputString = [inputString stringByReplacingOccurrencesOfString:@"*" withString:@""]; //need to remove *
    inputString = [inputString stringByReplacingOccurrencesOfString:@"~" withString:@""]; //need to remove ~
    NSMutableArray *arrayOfNumbers = [[inputString componentsSeparatedByString:@"_"] mutableCopy]; //

    int objects = [arrayOfNumbers count]; //no need for ~ sign

    for(int i = 0; i < objects; i++) //limit the message length as well
    {
        switch(i)
        {
            case 0: //header
                Header = [arrayOfNumbers objectAtIndex:i]; //[output characterAtIndex:0]; //fast conversion from String to char
                break;
            case 1: //ID
                ID = [[arrayOfNumbers objectAtIndex:i] intValue]; //[output intValue]; //toInt() returns long...not int; perfect for conductivity
                break;
            case 2: //Probe Type
                probeType = [[arrayOfNumbers objectAtIndex:i] intValue];
                break;
            case 3: //Probe Value
                probeValue = [[arrayOfNumbers objectAtIndex:i] doubleValue];
                break;
            case 4: //Temperature Type
                tempType = [[arrayOfNumbers objectAtIndex:i] intValue];
                break;
            case 5: //Temperature Value
                tempValue = [[arrayOfNumbers objectAtIndex:i] doubleValue];
                break;
            case 6: //Average
                average = [[arrayOfNumbers objectAtIndex:i] intValue];
                break;
            case 7:
                setGet = [[arrayOfNumbers objectAtIndex:i] intValue];
                break;
        }
        [self alert: [arrayOfNumbers objectAtIndex:i]];
    }
}
于 2013-07-22T15:51:43.013 回答