Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString to UTF8 conversion

CString to UTF8 conversion

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++ios
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vytheese
    wrote on last edited by
    #1

    I am a C# developer, recently I got to do with a VC++ 5.0 project, I stuck up with the following situation, Saving a string(CString) to file which should be saved as UTF-8 encoded. void CPrintMakerDlg::SaveHTML(LPCSTR fileName,CString fileContent) { ofstream iofile; try { LPCSTR str = fileContent.GetBuffer(fileContent.GetLength()); iofile.open(fileName, ios::in | ios::trunc |ios::binary ); iofile.write(str,fileContent.GetLength()); } catch(...) { AfxMessageBox("The problem is on saving file"); } iofile.close(); } I heard the CString will be using internally multi-byte character, So I expecting the file format also should be the UTF8 format but its saving as a ASCII. Thanks, Vythees

    B 1 Reply Last reply
    0
    • V vytheese

      I am a C# developer, recently I got to do with a VC++ 5.0 project, I stuck up with the following situation, Saving a string(CString) to file which should be saved as UTF-8 encoded. void CPrintMakerDlg::SaveHTML(LPCSTR fileName,CString fileContent) { ofstream iofile; try { LPCSTR str = fileContent.GetBuffer(fileContent.GetLength()); iofile.open(fileName, ios::in | ios::trunc |ios::binary ); iofile.write(str,fileContent.GetLength()); } catch(...) { AfxMessageBox("The problem is on saving file"); } iofile.close(); } I heard the CString will be using internally multi-byte character, So I expecting the file format also should be the UTF8 format but its saving as a ASCII. Thanks, Vythees

      B Offline
      B Offline
      bob16972
      wrote on last edited by
      #2

      vytheeswaran wrote:

      I heard the CString will be using internally multi-byte character, So I expecting the file format also should be the UTF8 format

      MBCS and UTF-8 are two different beasts. If your machine is using a loaded code page like Latin-1, I don't believe MBCS will contain any lead bytes that would lead to a double byte sequence anyway. The other western encodings might have them but I've never seen one in action in the US.

      vytheeswaran wrote:

      but its saving as a ASCII

      Are you sure it's limiting output to ASCII. It's more likely what some refer to as ANSI. Try outputing a character above index 127. Did the output resemble what MSDN lists for ANSI? Character sets[^] Transformation formats[^] UTF[^]

      V 1 Reply Last reply
      0
      • B bob16972

        vytheeswaran wrote:

        I heard the CString will be using internally multi-byte character, So I expecting the file format also should be the UTF8 format

        MBCS and UTF-8 are two different beasts. If your machine is using a loaded code page like Latin-1, I don't believe MBCS will contain any lead bytes that would lead to a double byte sequence anyway. The other western encodings might have them but I've never seen one in action in the US.

        vytheeswaran wrote:

        but its saving as a ASCII

        Are you sure it's limiting output to ASCII. It's more likely what some refer to as ANSI. Try outputing a character above index 127. Did the output resemble what MSDN lists for ANSI? Character sets[^] Transformation formats[^] UTF[^]

        V Offline
        V Offline
        vytheese
        wrote on last edited by
        #3

        Thank you for your links and I open the file in binary mode and append the UTF-8 BOBM EF BB BF in the starting posistion of the file. solved the problem. Grazie, Vythees

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups