Reputation: 16327
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 ?
Upvotes: 2
Views: 2623
Reputation: 3294
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: @""];
Upvotes: 1
Reputation: 4007
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;
}
Upvotes: 2