2010-09-13 9 views
0

Comment puis-je convertir une chaîne au format hex pour récupérer un NSData et ensuite un UIImage à l'intérieur de mon application iPhone?convertir une chaîne hexadécimale en un NSData

J'ai une chaîne (str) qui contient une valeur comme X'FFD8FFE000104A46 .... 01010000010001000 '.

Cette chaîne est créée à partir d'un fichier xml par la méthode:

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

Comment puis-je obtenir l'image de retour de cette chaîne?

Merci à l'avance,

Andrea

+0

Veuillez reformuler. Donne des exemples. –

+0

J'ai modifié la réponse –

Répondre

0

Pour convertir NSData à un NSString, essayez "initWithData: encodage:"

Pour convertir NSData à un UIImage, essayez "imageWithData:"

0

mon code d'échantillon, mis à jour!

-(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 une instruction switch pour cela? – C0deH4cker

+0

@ Mise à jour C0deH4cker – yarshure

Questions connexes