Search code examples
iphoneobjective-cstringclient-serveruint

How to convert uint8[] to string?


Hi I am doing work on an iPhone application I am getting some response of server in my buffer with data type Uint8 now i want to Match my buffer with a string, can any body tell me how can i check it? a piece of code is given

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{

    CFDataRef df = (CFDataRef) data;
    int len = CFDataGetLength(df);
    if(len <= 0) return;

    CFRange range = CFRangeMake(0,len);
    UInt8 buffer[len];
    NSLog(@"Received %d bytes from socket %d\n", 
          len, CFSocketGetNative(s));
    CFDataGetBytes(df, range, buffer);
    NSLog(@"Client received: %s\n", buffer); 

    NSLog(@"As UInt8 coding: %@", df);
    [mTextViewAlias setText:[NSString stringWithUTF8String: buffer]];
    ////////////.............////////////
    LoginViewController *lvc = [[LoginViewController alloc] init];

    NSString *search = @"ACPT";

    NSRange match;

    match = [buffer rangeOfString: search];

    if (match.location == NSNotFound)
        NSLog (@"Invalid Username Or Password");
    else
    {
        [lvc goToWatchList];

        NSLog (@"match found at index %i", match.location);
    }
}

any body have some idea? i'll be very thankful


Solution

  • Your response may be UInt8 but you don't need to copy it into a UInt8[] as your already have it in a CFDataRef (assuming the void * you pass in really is a CFDataRef, if it isn't you have other problems...) - a CFDataRef is toll-free bridged with NSData (i.e. you can just cast from one to the other), and you can construct an NSString directly from the bytes in an NSData. Your code, removing the logging and adding direct NSString conversion:

    void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
    {
        LoginViewController *lvc = [[LoginViewController alloc] init];
    
        NSString *buffer = [[NSString alloc] initWithData:(NSData *)data
                                                 encoding:NSUTF8StringEncoding];
        NSRange match = [buffer rangeOfString:@"ACPT"];
        [buffer release];
    
        if (match.location == NSNotFound)
            NSLog (@"Invalid Username Or Password");
        else
            [lvc goToWatchList];
    }
    

    Alternatively as, at least in your sample code: are not saving the converted buffer, are using a literal string for the search, and your use of %s implies the data is null-terminated; you could do all this directly with C functions without creating intermediate objects:

    void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
    {
        LoginViewController *lvc = [[LoginViewController alloc] init];
    
        char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data);
    
        if (strstr(buffer, "ACPT") == NULL)
            NSLog (@"Invalid Username Or Password");
        else
            [lvc goToWatchList];
    }