2

I am converting the APN Device token which is in NSData format to NSString, but i am some special characters,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSLog(@"Device Token 111 : %@", deviceToken);

    NSString *deviceStr = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
    NSLog(@"Device Token : %@", deviceStr);
    [deviceStr release];
}

Device Token 111 : <d8b62879 48de8f9f 90507519 da1d39cf 1b700f7f 022dcaf4 7532a8b7 a6f9afe4>
Device Token : ض(yHÞPuÚ9Ïep-Êôu2¨·¦ù¯ä

I have even tried with NSASCIIStringEncoding. What am i doing wrong ?

Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • 1
    Try iterating over the bytes of the NSData*, cast them to char's and log them, see if they come out as ` – Dan F Jun 02 '11 at 13:52
  • In your case the token ***is*** `ض(yHÞPuÚ9Ïep-Êôu2¨·¦ù¯ä` in unicode. The token isn't a string stored in an `NSData` object you know? It's also different from the device's serial number. Read the documentation. –  Jun 02 '11 at 13:59
  • Why are you converting the token to a string? The token is encrypted data, so it's just meaningless bytes, not meaningful text: "APNs generates a device token using information contained in the unique device certificate. The device token contains an identifier of the device. It then encrypts the device token with a token key and returns it to the device" ([here](http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW12)). – Jeremy W. Sherman Jun 02 '11 at 14:33

2 Answers2

2

Try using the following method with [deviceToken bytes] as the first parameter.

const static char hexchar[] = "0123456789ABCDEF";
- (NSString*) bytes2hex:(const char* ) buffer length:(int)buf_len {
    size_t i;
    char *p;
    int len = (buf_len * 2) + 1;
    p = malloc(len);
    for (i = 0; i < buf_len; i++) {
        p[i * 2] = hexchar[(unsigned char)buffer[i] >> 4 & 0xf];
        p[i * 2 + 1] = hexchar[((unsigned char)buffer[i] ) & 0xf];
    }
    p[i * 2] = '\0';
    NSString * result = [NSString stringWithCString:p encoding:NSUTF8StringEncoding];
    free(p); 
    return result; 
}
Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
  • 3
    Why would you rewrite the `%hhx` (format byte as hex) `printf` format specifier instead of just using `snprintf`? Even simpler would be to use `-[NSMutableString appendFormat:]` with the same format specifier. No `malloc`, no `free`, no dependence on the character set used to represent C string literals - just Foundation methods. – Jeremy W. Sherman Jun 02 '11 at 14:37
1

You may use description method to get the string representation of a token

NSLog(@"Token: [%@]", [devToken description]);

To remove non-numerical characters you may do:

NSCharacterSet *set = [[NSCharacterSet alphanumericCharacterSet] invertedSet];    
NSString *tkn = [[[devToken description] componentsSeparatedByCharactersInSet:set] componentsJoinedByString: @""];
dmitri
  • 3,183
  • 23
  • 28