UTF8String vs NSASCIIStringEncoding

Discussion in 'Mac Programming' started by poppy11, Oct 30, 2013.

  1. poppy11 macrumors newbie

    Joined:
    Oct 30, 2013
    #1
    I have a string msMyString that was created by

    NSMutableString *msMyString = [NSMutableString stringWithString:mad:"NoSpecialCharactersJustAnOrdinaryString"];

    Both of the lines below work perfectly, and so I am not at all certain which is preferred, etc. I know about NSLog(@"%@",msMyString), but I am trying to sort through all of the various ways of converting back and forth between objects, and 'C' primitives. I believe that I put that correctly, but I assume that you know what I mean.

    NSLog(@"%s",[msMyString cStringUsingEncoding:NSASCIIStringEncoding]);

    NSLog(@"%s",[msMyString UTF8String]);


    Thanks in advance.
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    If at all possible, stay away from anything other than UTF8.

    NSASCIIStringEncoding will break (return NULL) whenever there is a non-ASCII character in the string. There is no reason to take the risk. Only use cStringUsingEncoding: if you actually for whatever strange reason need a string in that particular encoding. Since NSLog expects UTF8 strings, any encoding that isn't a subset of UTF8 will produce strange results.
     
  3. devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #3
    not exactly, you encapsulate primitives inside object containers if that makes any sense? for the most part an "NSNumber" is the object to store primitive c types. for your question, the encoding really comes into play if you are parsing say a text file or some sort in UTF8 formatted file to an NSString object. a real world example would be a .csv file. those are usually in UTF8 encoding and converting to a NSString requires you specify that so it can properly "decode" that data and format it correctly as a string object. for your code example, you could have just as easily done this

    Code:
     NSMutableString *msMyString = @"NoSpecialCharactersJustAnOrdinaryString"; 
    and ended up with the same result. typically you would have no need to pipe out the various version of a NSString to a console as the regular "%@" option works fine for most cases. if you want to export that string out to a file or across the interwebs, that's when those encoding methods would come in handy.
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    That should be NSString* msMyString = @"xxx";

    The right hand side is not a mutable string, so you can't assign it to an NSMutableString*.
     
  5. poppy11 thread starter macrumors newbie

    Joined:
    Oct 30, 2013
    #5
    So I take it that the only reason that NSASCII... didn't fail is because it was purely ASCII text.


    Thanks for the information!
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    Which is perfectly reasonable if non-ASCII characters are illegal in your particular usage (base64 decoding, hex input, airline record locator ID, etc.)
     

Share This Page