NSString to char*

Discussion in 'Mac Programming' started by sujithkrishnan, Jun 5, 2008.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    Hi all.

    In my Iphone application, i successfully converted NSString to const char* using
    const char* cString = [nsStringObject cStringUsingEncoding:ASCIIEncoding];
    and i made use of the printf("%s",cString) to display it in console...
    But its printing only the first letter, not the entire string..
    (I am a little weak in pointers)
    Is it because of any null termination problems??
    Kindly help me in resolving this..

    Also i want to know "Why NSLog(nsStringObject) is not printing anything in console?

    I wnat to see my NSException reason (which is of NSString type)
     
  2. Sbrocket macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #2
    NSLog() prints messages to the debugger window in Xcode as of Beta 5 or 6, not the Console logs.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    I can't say immediately what might cause this, but that encoding doesn't look valid.

    The list of NSStringEncoding types is here:
    http://developer.apple.com/document...g.html#//apple_ref/doc/c_ref/NSStringEncoding

    I would expect an error if you used an encoding that is not supported, but maybe ASCIIEncoding is set to some value that matches up to a Unicode or other 16-bit encoding, and so the result you are getting would be:
    Code:
    cString[0] = 'M'
    cString[1] = '\0'
    cString[2] = 'y'
    cString[3] = '\0'
    cString[4] = ' '
    cString[5] = '\0'
    cString[6] = 't'
    cString[7] = '\0'
    cString[8] = 'e'
    cString[9] = '\0'
    cString[10] = 's'
    cString[11] = '\0'
    cString[12] = 't'
    cString[13] = '\0'
    for an NSString containing "My test". Try UTF8String instead of cStringUsingEncoding, or pick a different encoding from that list that you know is 8-bit per character. Otherwise, you cannot use any of the standard C functions that deal with "character strings", due to the reliance on a null terminator, etc.

    -Lee
     
  4. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #4
    My question is: are you really using the value ASCIIEncoding as the encoding type? If so, it isn't what you want. You want NSASCIIEncoding instead.
     
  5. white89gt macrumors regular

    Joined:
    Jan 18, 2006
    #5
    I believe you'll have better luck converting you NSString to UTF8. I don't have my Cocoa book in front of me, but I'll post up what it recommends when I get home this afternoon.
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    There is no identfier "ASCIIEncoding" in any header file, so this shouldn't compile. Do you have a variable named "ASCIIEncoding" anywhere? Or did you use for example kCFStringEncodingASCII (which is very wrong for NSString)?
     
  7. white89gt macrumors regular

    Joined:
    Jan 18, 2006
    #7
    I just checked my Cocoa Programming for Mac OS X book and Hillegas recommends the following way to convert NSStrings to c style strings.

    NSString *foo = @"your text here";
    const char *bar = [foo UTF8String];
     
  8. TeeJayEm macrumors regular

    Joined:
    Mar 28, 2008
    #8
    I can confirm, you should be using UTF8String
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    I think the OPs question has been answered many times over, it just happens to be 3:15am in Bangalore at the moment. Hopefully we've led them in the right direction.

    -Lee
     
  10. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #10


    Sorry...
    yeah.. i made use of NSASCIIEncoding...

    okie.. now i will try wityh UTF8Encoding .

    Thanks to all....

    Thank You Lee..

    I got it....
     
  11. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #11
    You can't be using NSASCIIEncoding because there is no NSASCIIEncoding. By asking for help and being too careless to give actual correct information you are just wasting everyone's time.
     
  12. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #12
    i meant ASCIIEncoding
    Sorry for the mistake..
     
  13. valexa macrumors member

    Joined:
    Nov 3, 2007
    #13
    he means NSASCIIStringEncoding .. he's very confused
     
  14. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    Why are you using ASCII encoding? This will prevent your code from working with lots and lots of possible data. If you need a char*, I would really recommend that you go for UTF8.
     

Share This Page