Problem with string encoding

Discussion in 'Mac Programming' started by sandeepac, Jul 10, 2008.

  1. sandeepac macrumors newbie

    sandeepac

    Joined:
    Jul 10, 2008
    Location:
    Cochin
    #1
    Sir

    I have a problem with encoding of strings

    strcpy(myCString, [MyNSString cStringUsingEncoding:NSUnicodeStringEncoding ]);

    CFStringRef str1 = CFStringCreateWithCString(NULL,myCString,kCFStringEncodingUnicode);
    NSLog((NSString*)str1);

    if my unicode string is मनज मानमान मानमा.
    then i am getting Only मनज as output.
    can you please help me to solve this problem.


    Thanking you

    Sandeep
     
  2. Enuratique macrumors 6502

    Joined:
    Apr 28, 2008
    #2
    Try changing your logging code to

    Code:
    NSLog(@"%@", str1);
    
    and see if you get the expected string back.
     
  3. sandeepac thread starter macrumors newbie

    sandeepac

    Joined:
    Jul 10, 2008
    Location:
    Cochin
    #3
    Still the Result is Same ............
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    That cannot possibly work. C strings are for strings using 8 bit characters. NSUnicodeStringEncoding uses the 16 bit Unicode encoding. As soon as you have a character with a Unicode value from 0 to 255, strcpy () sees a zero byte and thinks that is the end of the string. Same if you have a character with a Unicode value like 256, 512, 768 and so on.

    You need to use the appropriate UTF8 encodings. UTF8 encodes Unicode in eight bit values in a way that is compatible with C string functions.

    In this case, the whole conversion is not needed anyway. Go to www.developer.com and look for "toll free bridging". Might save you lots of work and code and memory and execution time and problems when you mix Cocoa and Core Foundation.
     

Share This Page