2010-09-13 49 views
0

如何轉換十六進制格式的字符串以從中檢索NSData,然後在我的iPhone應用程序中使用UIImage?將十六進制字符串轉換爲NSData

我有一個字符串(str),其中包含一個像X'FFD8FFE000104A46 .... 01010000010001000'的值。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    str = [str stringByAppendingString:string]; 
} 

我怎樣才能獲得的圖像從該字符串返回:

該字符串從XML文件通過的方法創建?

由於提前,

安德烈

+0

請更改。舉例說明。 – 2010-09-13 16:47:26

+0

我編輯了答案 – 2010-09-15 06:57:43

回答

0

對於NSData的轉換成的NSString,嘗試 「initWithData:編碼:」

對於NSData的轉換成的UIImage,嘗試 「imageWithData:」

0

我的示例代碼已更新!

-(NSData*)toString:(NSString*)uniStr 
{ 
    int len=[uniStr length]/4; 
    unichar *oux=alloca(len*sizeof(unichar)); 
    unichar *p = oux; 

    for (int i=0; i<[uniStr length]/4; i++) { 


     NSInteger first= i*4; 
     NSUInteger a =toInde([uniStr characterAtIndex:first])<<4;//3 3c 
     NSUInteger b =toInde([uniStr characterAtIndex:(first+1)]);//2 
     NSUInteger c =toInde([uniStr characterAtIndex:(first+2)])<<12 ;//0 68 
     NSUInteger d =toInde([uniStr characterAtIndex:(first+3)])<<8;//0 

     unichar x = a+b+c+d;//十進制 3c68->683c 
     memcpy(p, &x, 2); 
     p++; 

    } 

    return [NSData dataWithBytes:oux length:len*2]; 
} 

int toInde(int x) 
{ 

    char b=0; 
    sprintf(&b, "%c",a); 
    return b; 
} 
+1

Ewwww一個switch語句? – C0deH4cker 2013-02-15 03:50:26

+0

@ C0deH4cker更新 – yarshure 2014-07-02 15:40:47

相關問題