zoukankan      html  css  js  c++  java
  • iphone 中文乱码解决方案

    //NSData 转 NSString

    NSData *data;

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

    //NSString  转 NSData

    NSString *string;

    NSData*data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    //char 转 NSString

    char *str1;

    NSString*str = [NSString stringWithCString:str1 encoding:NSUTF8StringEncoding];

    //NSString 转 char

    NSString *str;

    char *str1 = [str UTF8String];

    //自转换

    NSString*string;

    string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //也可以修改NSString类

    //NSString+URLEncoding.h

    #import<Foundation/Foundation.h>

    @interface NSString (OAURLEncodingAdditions)

    - (NSString *)URLEncodedString;

    - (NSString *)URLDecodedString;

    @end

    //NSString+URLEncoding.m

    #import"NSString+URLEncoding.h"

    @implementation NSString (OAURLEncodingAdditions)

    - (NSString *)URLEncodedString 

    {

       NSString*result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,

                                                                               (CFStringRef)self,

                                                                               NULL,

      CFSTR("!*'();:@&=+$,/?%#[]"),

                                                                               kCFStringEncodingUTF8);

        [result autorelease];

    return result;

    }

    //解决url乱码

    - (NSString*)URLDecodedString

    {

    NSString*result = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,

      (CFStringRef)self,

      CFSTR(""),

      kCFStringEncodingUTF8);

        [result autorelease];

    return result;

    }

    @end

  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/zhwl/p/2630112.html
Copyright © 2011-2022 走看看