NSLog incorrect encoding

前端 未结 4 2058
别跟我提以往
别跟我提以往 2020-11-29 06:44

I\'ve got a problem with the following code:

NSString *strValue=@\"你好\";
char temp[200];
strcpy(temp, [strValue UTF8String]);
printf(\"%s\", temp);
NSLog(@\"         


        
相关标签:
4条回答
  • 2020-11-29 07:20

    I know you are probably looking for an answer that will help you understand what's going on.

    But this is what you could do to solve your problem right now:

    NSLog(@"%@", strValue);
    
    0 讨论(0)
  • 2020-11-29 07:41

    NSLog's %s format specifier is in the system encoding, which seems to always be MacRoman and not unicode, so it can only display characters in MacRoman encoding. Your best option with NSLog is just to use the native object format specifier %@ and pass the NSString directly instead of converting it to a C String. If you only have a C string and you want to use NSLog to display a message instead of printf or asl, you will have to do something like Don suggests in order to convert the string to an NSString object first.

    So, all of these should display the expected string:

    NSString *str = @"你好";
    const char *cstr = [str UTF8String];
    NSLog(@"%@", str);
    printf("%s\n", cstr);
    NSLog(@"%@", [NSString stringWithUTF8String:cstr]);
    

    If you do decide to use asl, note that while it accepts strings in UTF8 format and passes the correct encoding to the syslog daemon (so it will show up properly in the console), it encodes the string for visual encoding when displaying to the terminal or logging to a file handle, so non-ASCII values will be displayed as escaped character sequences.

    0 讨论(0)
  • 2020-11-29 07:42

    My guess is that NSLog assumes a different encoding for 8-bit C-strings than UTF-8, and it may be one that doesn't support Chinese characters. Awkward as it is, you might try this:

    NSLog(@"%@", [NSString stringWithCString: temp encoding: NSUTF8StringEncoding]);
    
    0 讨论(0)
  • 2020-11-29 07:45
        #   define NSLogUTF8(a,b) NSLog(a,[NSString stringWithCString:[[NSString stringWithFormat:@"%@",b] cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding])
    
    #define NSLogUTF8Ex(a,b) NSLog(a,[MLTool utf8toNString:[NSString stringWithFormat:@"%@",b]])
    
    +(NSString*)utf8toNString:(NSString*)str{
        NSString* strT= [str stringByReplacingOccurrencesOfString:@"\\U" withString:@"\\u"];
        //NSString *strT = [strTemp mutableCopy];
        CFStringRef transform = CFSTR("Any-Hex/Java");
        CFStringTransform((__bridge CFMutableStringRef)strT, NULL, transform, YES);
    
        return strT;
    }
    
    0 讨论(0)
提交回复
热议问题