iOS NSString 与NSData转化

1. 字符串转Data

NSString * str =@"str";

NSData *data =[str dataUsingEncoding:NSUTF8StringEncoding];

2.NSData 转NSString

NSString * str  =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

3.data 转char

NSData *data;

char * haha=[data bytes];

4. char 转data

byte * byteData = malloc(sizeof(byte)*16);

NSData *content=[NSData dataWithBytes:byteData length:16];

时间: 2024-12-29 23:47:14

iOS NSString 与NSData转化的相关文章

iOS NSString 和NSData 转换

NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; NSData 转换成NSString对象 NSData * data; NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding]; NSData 转换成char* NSData *dat

iOS中NSString,NSData和char的相互转换,JSON串转化为NSDictionary或NSArray

1.NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; 2.NSData 转换成NSString对象 NSData * data; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 3.NSData 转换成char* NSData

iOS NSDictionary、NSData、JSON等 数据类型相互转换

1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"balance", @"key", @"remaining balance", @"label", @"45", @"value", @"USD", @"curre

NSString、NSData、char* 类型之间的转换-备

1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length])); 与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节 2. NSString 转化为 char * NSString * str=

How convert devicetoken (NSString) into NSData

deviceToken是个nsdata (nslog like this:<35b79913 2f94bf8d 3f7a863f 398410ca d6c61428 bae03de4 bf86b656 1b1042bf>)我把它转化为nsstring(35b79913 2f94bf8d 3f7a863f 398410ca d6c61428 bae03de4 bf86b656 1b1042bf)后怎么再转换为nsdata(要nslog 依然是:<35b79913 2f94bf8d 3f7a

iOS: [NSString hash]出现同样的hash值问题

问题原因: At least there are special circumstances for which this unreliability kicks in. Comparing [a hash] and [b hash] of two different NSString is safe when: the strings' length is shorter or equal to 96 characters. [a length] is different to [b leng

ios NSString常见的字符串操作 分割 查找

1.NSString *str = [[NSString alloc]init];     //简单粗暴,基本用不到 2.NSString *str = [[NSString alloc]initWithFormat:@"xxx%@", str]; //最喜欢的常见方式了 3.NSString *str = [[NSString alloc]initWithString:str]; 4.NSString *str = [[NSString alloc]initWithNSData:(N

iOS NSString的学习熟悉

对于字符串频繁的切割,合并,插入等操作,优先使用NSMutableString类. 这里讨论的是NSString.然后就是贴代码: 1 -(void)stringDemo{ 2 NSLog(@"____________________________"); 3 4 NSString *string = [[NSString alloc] initWithString:@"Hi,woods!"]; 5 NSLog(@"%@", string); 6

iOS NSString的常用用法

//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring); // NSString *astring = [[NS