How to Convert My Device Token (Nsdata) into an Nsstring

How can I convert my device token (NSData) into an NSString?

Note - This will not work when compiling with the iOS 13 or later SDK

use this :

NSString * deviceTokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];

NSLog(@"The generated device token string is : %@",deviceTokenString);

How to change the device token from NSData to NSString

You are saving NSData to NSUserDefaults, you need to convert the NSData to NSString first and save that to the NSUserDefaults.

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [deviceToken description];
token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"DeviceToken : %@", token);
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isNotificationsEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];

}

How to convert iOS Device Token String back to NSData

I got it! I was able to do this using this extremely helpful link:

http://iphoneappcode.blogspot.com/2012/04/nsdata-to-hexstring-and-hexstring-to.html

- (NSData *)dataFromHexString:(NSString *)string
{
NSMutableData *stringData = [[[NSMutableData alloc] init] autorelease];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [string length] / 2; i++) {
byte_chars[0] = [string characterAtIndex:i*2];
byte_chars[1] = [string characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[stringData appendBytes:&whole_byte length:1];
}
return stringData;
}

And then:

NSData *deviceToken = [self dataFromHexString:myStringToken];

:)

Iphone device token - NSData or NSString

Ok, I found a solution. If anyone has the same problem, forget about ASCII encoding, just make the string with the following lines:

NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];

How to Convert NSData to NSString

I am posting this for records sake because I found a duplicate and voting to close this down.

Actually what I am receiving is a stream of bytes represented as hex, and all the answers indicated do not work. Only [NSData description] gave me true data, which is something I can't use because it is intended for debugging.

Finally I tried the solution given here, and I get what I want.
Thanks to all for trying to help out.

NSData to NSString in ObjectiveC

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;
}

How to convert NSData deviceToken to JSON-compatible string to remote API

My app use this method below, I don't think it is a perfect way for it but it works

NSString *strToken = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] ;

Getting the iOS device token for push notifciations in RubyMotion

I've been trying to think of a pure RubyMotion solution, but I just don't understand 'Pointers' enough to get it to work. So I've gone a different way: extending the NSData class.

1) Create the folder vendor/NSData+Hex inside your project.

2) Create the file NSData+Hex.h inside the folder and put this inside:

#import 

@interface NSData (Hex)

@property (nonatomic, copy, readonly) NSString *hexString;

@end

3) Now create NSData+Hex.m in the same folder and put this in the file:

#import "NSData+Hex.h"

@implementation NSData (Hex)

- (NSString *) hexString
{
NSUInteger len = self.length;
if (len == 0) {
return nil;
}
const unsigned char *buffer = self.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(len * 2)];
for (int i = 0; i < len; ++i) {
[hexString appendFormat:@"%02x", buffer[i]];
}
return [hexString copy];
}

@end

4) Add this to your Rakefile:

app.vendor_project('vendor/NSData+Hex', :static)

5) Now you can simply call deviceToken.hexString within def application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken).

If anyone can think of a pure RubyMotion solution, feel free to answer. For now, it's at least working. :)

How to pass NSData as NSString and get it back?

Solved.

Using dataUsingEncoding, value of NSData it's different.

To pass data around methods or apps, etc, i've used base64 convertion.

Encode

NSString *d = 
[NSString stringWithFormat:@"appdue://obj=%@",
[APBase64Converter base64forData:data]];

Decode

NSData *data = [APBase64Converter base64DataFromString:urlParams];

APBase64Converter Is a lib that encode/decode easily data and strings.

Working example and APBase64Converter lib can be downloaded from here: http://goo.gl/8YNjS

Thanks all.

I "retain" this post for helps people and me next time!



Related Topics



Leave a reply



Submit