2017-07-29 1 views
0

Ich konvertiere NSString in Byte-Array. Es ist ok, dann konvertiere ich NSData zu base64 ist falsch. wenn "" ist richtig, aber mit hoher Nummer (Prüfung: @ "333435363738") ist falsch. Das ist mein Code. Bitte hilf mir.Convert NSData in Base-64 falsch

In Android: ISIjJCUm und iOS: MzQ1Njc4.

NSString *command = @"333435363738"; 
NSMutableData *commandToSend= [[NSMutableData alloc] init]; 
unsigned long whole_byte; 
char byte_chars[3] = {'\0','\0','\0'}; 
int i; 
for (i=0; i < [command length] /2; i++) { 
    NSLog(@"%d",[command characterAtIndex:i*2]); 
    NSLog(@"%d",[command characterAtIndex:i*2 + 1]); 
    byte_chars[0] = [command characterAtIndex:i*2]; 
    byte_chars[1] = [command characterAtIndex:i*2 + 1]; 
    whole_byte = strtol(byte_chars, NULL, 16); 
    [commandToSend appendBytes:&whole_byte length:1]; 
} 



NSString *base64String; 
if ([commandToSend respondsToSelector:@selector(base64EncodedStringWithOptions:)]) { 
    base64String = [commandToSend base64EncodedStringWithOptions:kNilOptions]; // iOS 7+ 
} else { 
    base64String = [commandToSend base64Encoding];        // pre iOS7 
} 

Antwort

1

Ihr Code erzeugt die Zeichenfolge, die die MzQ1Njc4 bas64 Codierung des Bytes ist 0x33, 0x34, 0x35, 0x36, 0x37, 0x38. Dies scheint der Code zu sein.

Der String ISIjJCUm ist die Base64-Kodierung von 0x21, 0x22, 0x23, 0x24, 0x25, 0x26.

Beachten Sie, dass 0x2133 dezimal ist. Es sieht also so aus, als ob Sie die Zeichenfolge entweder als Dezimalzahl für iOS oder als Hexadezimal für Android interpretieren sollten.