2011-04-19 823 views
0

嗨 我正在做一個iPhone應用程序的工作 我收到一些響應服務器在我的緩衝區中的數據類型Uint8 現在我想匹配我的緩衝區與字符串,任何機構可以告訴我我怎麼檢查它?一段代碼被賦予如何將uint8 []轉換爲字符串?

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); 
    } 
} 

任何機構有一些想法? 我會很感激出現

+0

在此行「match = [buffer rangeOfString:search];」這裏有一個警告「無效接收器類型」Uint8 [1]'「 – Mashhadi 2011-04-19 06:33:57

回答

2

你的反應可能是UInt8但你並不需要把它複製到UInt8[]因爲你已經擁有了它在一個CFDataRef(假設void *你真傳是一個CFDataRef,如果不是你有其他問題...) - 一個CFDataRef免費橋接NSData(即你可以從一個到另一個),並且你可以直接從NSData的字節構造一個NSString。您的代碼,刪除記錄,並直接加入的NSString轉換:

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]; 
} 

或者作爲,至少在你的代碼示例:不保存轉換的緩衝,使用一個字符串的搜索,和您的%s使用意味着數據是空終止的;您可以直接使用C函數完成所有這些操作,而無需創建中間對象:

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]; 
} 
+0

哇,這是完美的我是非常感謝你:) – Mashhadi 2011-04-19 11:15:12

0

的警告,因爲rangeOfStringNSStringbuffer的方法不是NSString。我認爲如果你能把數據轉換成NSString會更好。我想如果你嘗試使用stringWithData:

UPDATE

請選中該Link

+0

thanx的答覆,你可以告訴我一些細節,如何將uint8轉換爲字符串?如何使用StringWithData:? – Mashhadi 2011-04-19 07:09:30

+0

請檢查我的更新回答 – visakh7 2011-04-19 07:14:40

+0

:(沒有任何工作 – Mashhadi 2011-04-19 07:32:11

1

您是否在尋找這一點,是否行得通呢?

NSString *string = [[NSString alloc] 
    initWithData:(id)data 
    encoding:NSUTF8StringEncoding]; 
+0

嗯有些事情是這樣的 感染我想分配我的Uint8數組中的數據到一個字符串,但我沒有得到它 – Mashhadi 2011-04-19 07:31:41

+0

因爲你已經有數據作爲'NSData'在'data'變量,爲什麼你不創建字符串? – zoul 2011-04-19 08:39:34

+0

Ahhh哇是多麼愚蠢我爲什麼我不看數據lolzzzz 非常感謝你Zoul :)我知道了 – Mashhadi 2011-04-19 11:07:09