luckymascot

Q: setlocale(LC_ALL, en_US.UTF-8) failing on ipad but working fine on simulator

While converting a char* to wchar*, i am setting the locale as en_US.UTF-8. The setlocale function returns NULL on iPad due to which incorrect string is displayed. I also tried "en_US"  locale but the converted wchar string is still incorrect and is same as it was in the case of locale failure. I don't know if i am missing something. Please help.

iPad, iOS 5.0.1

Posted on Dec 15, 2011 5:42 AM

Close

Q: setlocale(LC_ALL, en_US.UTF-8) failing on ipad but working fine on simulator

  • All replies
  • Helpful answers

  • by etresoft,

    etresoft etresoft Dec 15, 2011 8:17 AM in response to luckymascot
    Level 7 (29,380 points)
    Mac OS X
    Dec 15, 2011 8:17 AM in response to luckymascot

    Why would you ever use wchar *?

  • by luckymascot,

    luckymascot luckymascot Dec 15, 2011 6:47 PM in response to etresoft
    Level 1 (1 points)
    Dec 15, 2011 6:47 PM in response to etresoft

    I need it in a C library where I need to convert char* to uniode string. For this conversion i am using mbstowcs(), but it requires locale to be set.

    What locale i need to set on iPad for correct unicode conversion?

  • by etresoft,

    etresoft etresoft Dec 16, 2011 2:45 PM in response to luckymascot
    Level 7 (29,380 points)
    Mac OS X
    Dec 16, 2011 2:45 PM in response to luckymascot

    I know the reason for the wchar. My question was more rhetorical to make you think.

     

    You have Cocoa on the iPad. All of your user-generated string input is already in NSStrings that fully support Unicode. There are NSString methods to convert to UTF8 C strings if you need them. If you are getting data from some internet source, it will likely already be in UTF8.

     

    In short, there is no reason to use wchar unless you just really want to do 3 times the work and you enjoy punishing yourself.

  • by heckmeck,

    heckmeck heckmeck Oct 4, 2016 11:29 AM in response to luckymascot
    Level 1 (4 points)
    Oct 4, 2016 11:29 AM in response to luckymascot

    Dear luckymascot,

     

    it's been a while - have you ever figured out how to solve this ?

     

    I am trying to utilise strftime() to achieve a portable (Android/iOS) implementation of certain calendar functions.

    For localisation, I use setlocale(LC_TIME, "fr_FR") (e.g.) - which works beautifully with the iOS simulator, but fails on any iOS device. - On devices, setlocale() always returns NULL ...