You ask why this didn't work:
NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
That's because the initWithData
assumes that the NSData
contains a UTF8 string. But in your case, the deviceToken
is not a UTF8 string; it is binary data.
The rest of your question presumes that you will create the <72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>
string (presumably that you created with stringWithFormat
or description
methods), and you're asking how to trim the <
, >
, and remove the spaces. (I think others have answered that question.)
I might suggest a different approach, though. You could simply have a routine to create the hexadecimal string directly, like so:
NSString *deviceString = [self hexadecimalStringForData:deviceToken];
You could then have a hexadecimalStringForData
method like so:
- (NSString *)hexadecimalStringForData:(NSData *)data
{
NSMutableString *hexadecimalString = [[NSMutableString alloc] initWithCapacity:[data length] * 2];
uint8_t byte;
for (NSInteger i = 0; i < [data length]; i++)
{
[data getBytes:&byte range:NSMakeRange(i, 1)];
[hexadecimalString appendFormat:@"%02x", byte];
}
return hexadecimalString;
}
You certainly can use the description
/stringWithFormat
approach and then clean up that string, as others have suggested, but the above strikes me as a more direct solution.