C++ Character Encoding in Mac

Joined
Jan 16, 2013
Messages
1
Reaction score
0
Points
1
I'm developing a Cross Platform File Sync Application.In Mac OS X to get File System events, i read from /dev/fsevents system buffer and send it over unix sockets to another app. I'm not doing any character encoding until now.

This is my Print in app which recieves the FS Events :
######## File Name ::: ébê123.rtf
######## File Name in WCHAR ::: ébê123.rtf

code which i used to convert char to wchar

int wCharLen1 = mbstowcs(NULL, fName, 0); // fName is the char which i recieved through unix socket
WCHAR* fileName = new WCHAR[wCharLen1 + 1];
memset(fileName,'\0',(wCharLen1 + 1) *sizeof(WCHAR));
mbstowcs(fileName, fName, wCharLen1);

I'm sending the file name to my Server and have printed the file name before DB Insert, which prints the exact file name :

######## Recieved File Name ::: ébê123.rtf


But in DB it inserts the file Name as 'e�bê123.rtf'


I'm using the same code in Windows except i don't have to do wchar conversion, because the Windows Directory Monitoring itself gives the file name in wchar. I don't have any issues with the windows client and the file name is inserted correctly in the database as ' ébê123.rtf '. I suspect that i'm missing some encoding before converting char to wchar in Mac. I have tried encoding to UTF-8 , but the file Names have changed to
######### FileName ::: ébê123.rtf after Encoding TO UTF-8 ::: eÌ�beÌ‚123.rtf [MAC]

Another Case :

When uploading files from Windows with the above file name 'ébê123.rtf' , the file gets downloaded in Mac with the correct file name. But when the file is uploaded from Mac , then the file name seems to be downloaded correctly in Windows, but as soon as i change anything in that file, the file name is sent as 'e%cc%81be%cc%82123.rtf' to Server,then to Mac. But if i originally create the file 'ébê123.rtf' in Windows, then it is sent correctly.

I suspect i have to encode the file name in mac to UTF-8 string before converting char to wchar in Mac. But i have tried some open source code like the one below :
Code:
  void latin1_to_utf8(unsigned char *in, unsigned char *out)
  {
    while (*in)
    {
      if (*in<128)
      {
        *out++=*in++;
      }
      else 
      {
        *out++=0xc2+(*in>0xbf);
        *out++=(*in++&0x3f)+0x80;
      }
    }
    *out = '\0';
  }


But it didn't worked. Now i'm looking for a library or some code to convert the string to utf-8 string in C++ in Mac.Of Course this function works when the file name is recieved from Windows to Mac. Any ideas ..?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Moved to more appropriate forum - this is a development question.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top