iOS十六进制和字符串的相互转换
感谢分享,亲测可用
- NSString *dictString = [dict JSONFragment];//组合成的。
用这个就行了。。。
- dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}===
- // 十六进制转换为普通字符串的。
- + (NSString *)stringFromHexString:(NSString *)hexString { //
- char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
- bzero(myBuffer, [hexString length] / 2 + 1);
- for (int i = 0; i < [hexString length] - 1; i += 2) {
- unsigned int anInt;
- NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
- NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] autorelease];
- [scanner scanHexInt:&anInt];
- myBuffer[i / 2] = (char)anInt;
- }
- NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
- NSLog(@"------字符串=======%@",unicodeString);
- return unicodeString;
- }
- //普通字符串转换为十六进制的。
- + (NSString *)hexStringFromString:(NSString *)string{
- NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
- Byte *bytes = (Byte *)[myD bytes];
- //下面是Byte 转换为16进制。
- NSString *hexStr=@"";
- for(int i=0;i<[myD length];i++)
- {
- NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
- if([newHexStr length]==1)
- hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
- else
- hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
- }
- return hexStr;
- }
posted on 2016-03-23 17:27 🌞Bob 阅读(379) 评论(0) 编辑 收藏 举报