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. utf to ascii conversion

utf to ascii conversion

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresperformancequestion
9 Posts 3 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.
  • K Offline
    K Offline
    khushboo gupta
    wrote on last edited by
    #1

    Hi, Can anybody suggest me how can i convert a string from utf8 (like wchar_t toTranscode[] = L"ناعسالاخخ";) format to ascii (like äÇÚÓÇáÇÎÎ).. I have tried with the following code ..but when i am trying to print char array pToFill it is blank.

    #include "stdio.h"
    #include <string.h>
    #include "stdlib.h"
    #define _CRT_SECURE_NO_WARNINGS

    int main()
    {
    FILE *p = NULL;
    size_t len = 0;
    unsigned int convertedChars = 0;

    int ret;
    
    wchar\_t toTranscode\[\] = L"ناعسالاخخ";
    //wchar\_t toTranscode\[\] = L"hello";
    unsigned int wLent = wcslen((const wchar\_t\*)toTranscode);
    //------------------------------------------------------
    // a UTF-8 character can have maximum 6 bytes
    //------------------------------------------------------
    int maxCharLen = 6\*wLent;
    wchar\_t\* pWideCharBuf;
    char\* pToFill;
    unsigned int i;
    
    //--------------------------------------------------------------
    // alloc memory for variables.
    //--------------------------------------------------------------
    pWideCharBuf = (wchar\_t\*)malloc(wLent+1);
    pToFill = (char\*)malloc(maxCharLen+1);
    
    for (i = 0; i < wLent; ++i)
    pWideCharBuf\[i\] = toTranscode\[i\];
    pWideCharBuf\[wLent\] = 0x00;
    
    memset (pToFill, 0, maxCharLen+1);
    

    // convert the string

    //ret = wcstombs(pToFill, pWideCharBuf, maxCharLen);
    
    ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen);
    //ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, \_TRUNCATE);
    printf("ret is %d\\n", ret);
    
    printf("the ascii char is  = %s\\n", pToFill);
    printf("to test\\n");
    
    
    p = fopen("hello.txt", "w");
    if (p== NULL) {
       printf("Error in opening a file..");
    }
    

    len = strlen(pToFill);
    fwrite(pToFill, len, 1, p);
    fclose(p);
    printf("\nWritten Successfuly in the file.\n");

    // pToFill contains the string
    
    //-------------------------------------------------------
    // clean-up
    //-------------------------------------------------------
    //lete \[\] pWideCharBuf ;
    
    // don't forget to
    //delete \[\] pToFill ;
    return 0;
    

    }

    please help

    L M 2 Replies Last reply
    0
    • K khushboo gupta

      Hi, Can anybody suggest me how can i convert a string from utf8 (like wchar_t toTranscode[] = L"ناعسالاخخ";) format to ascii (like äÇÚÓÇáÇÎÎ).. I have tried with the following code ..but when i am trying to print char array pToFill it is blank.

      #include "stdio.h"
      #include <string.h>
      #include "stdlib.h"
      #define _CRT_SECURE_NO_WARNINGS

      int main()
      {
      FILE *p = NULL;
      size_t len = 0;
      unsigned int convertedChars = 0;

      int ret;
      
      wchar\_t toTranscode\[\] = L"ناعسالاخخ";
      //wchar\_t toTranscode\[\] = L"hello";
      unsigned int wLent = wcslen((const wchar\_t\*)toTranscode);
      //------------------------------------------------------
      // a UTF-8 character can have maximum 6 bytes
      //------------------------------------------------------
      int maxCharLen = 6\*wLent;
      wchar\_t\* pWideCharBuf;
      char\* pToFill;
      unsigned int i;
      
      //--------------------------------------------------------------
      // alloc memory for variables.
      //--------------------------------------------------------------
      pWideCharBuf = (wchar\_t\*)malloc(wLent+1);
      pToFill = (char\*)malloc(maxCharLen+1);
      
      for (i = 0; i < wLent; ++i)
      pWideCharBuf\[i\] = toTranscode\[i\];
      pWideCharBuf\[wLent\] = 0x00;
      
      memset (pToFill, 0, maxCharLen+1);
      

      // convert the string

      //ret = wcstombs(pToFill, pWideCharBuf, maxCharLen);
      
      ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen);
      //ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, \_TRUNCATE);
      printf("ret is %d\\n", ret);
      
      printf("the ascii char is  = %s\\n", pToFill);
      printf("to test\\n");
      
      
      p = fopen("hello.txt", "w");
      if (p== NULL) {
         printf("Error in opening a file..");
      }
      

      len = strlen(pToFill);
      fwrite(pToFill, len, 1, p);
      fclose(p);
      printf("\nWritten Successfuly in the file.\n");

      // pToFill contains the string
      
      //-------------------------------------------------------
      // clean-up
      //-------------------------------------------------------
      //lete \[\] pWideCharBuf ;
      
      // don't forget to
      //delete \[\] pToFill ;
      return 0;
      

      }

      please help

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I get error 0x2A (EILSEQ) when trying to convert this string. According to MSDN[^] this means it has encountered a character that it cannot convert.

      Use the best guess

      K 1 Reply Last reply
      0
      • L Lost User

        I get error 0x2A (EILSEQ) when trying to convert this string. According to MSDN[^] this means it has encountered a character that it cannot convert.

        Use the best guess

        K Offline
        K Offline
        khushboo gupta
        wrote on last edited by
        #3

        Hi sir, Can u please suggest me then how can i convert Arabic characters stored in utf-8 format in database to ascii characters in c. Is there any built-in function through which i can perform conversion from utf-8 to ascii. Thanks

        L 1 Reply Last reply
        0
        • K khushboo gupta

          Hi sir, Can u please suggest me then how can i convert Arabic characters stored in utf-8 format in database to ascii characters in c. Is there any built-in function through which i can perform conversion from utf-8 to ascii. Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Try the following:

          \_locale\_t locale =  \_create\_locale(LC\_CTYPE, "Arabic");
          errno\_t ret = \_wcstombs\_s\_l(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen, locale);
          

          Use the best guess

          K 2 Replies Last reply
          0
          • K khushboo gupta

            Hi, Can anybody suggest me how can i convert a string from utf8 (like wchar_t toTranscode[] = L"ناعسالاخخ";) format to ascii (like äÇÚÓÇáÇÎÎ).. I have tried with the following code ..but when i am trying to print char array pToFill it is blank.

            #include "stdio.h"
            #include <string.h>
            #include "stdlib.h"
            #define _CRT_SECURE_NO_WARNINGS

            int main()
            {
            FILE *p = NULL;
            size_t len = 0;
            unsigned int convertedChars = 0;

            int ret;
            
            wchar\_t toTranscode\[\] = L"ناعسالاخخ";
            //wchar\_t toTranscode\[\] = L"hello";
            unsigned int wLent = wcslen((const wchar\_t\*)toTranscode);
            //------------------------------------------------------
            // a UTF-8 character can have maximum 6 bytes
            //------------------------------------------------------
            int maxCharLen = 6\*wLent;
            wchar\_t\* pWideCharBuf;
            char\* pToFill;
            unsigned int i;
            
            //--------------------------------------------------------------
            // alloc memory for variables.
            //--------------------------------------------------------------
            pWideCharBuf = (wchar\_t\*)malloc(wLent+1);
            pToFill = (char\*)malloc(maxCharLen+1);
            
            for (i = 0; i < wLent; ++i)
            pWideCharBuf\[i\] = toTranscode\[i\];
            pWideCharBuf\[wLent\] = 0x00;
            
            memset (pToFill, 0, maxCharLen+1);
            

            // convert the string

            //ret = wcstombs(pToFill, pWideCharBuf, maxCharLen);
            
            ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen);
            //ret = wcstombs\_s(&convertedChars, pToFill, maxCharLen, pWideCharBuf, \_TRUNCATE);
            printf("ret is %d\\n", ret);
            
            printf("the ascii char is  = %s\\n", pToFill);
            printf("to test\\n");
            
            
            p = fopen("hello.txt", "w");
            if (p== NULL) {
               printf("Error in opening a file..");
            }
            

            len = strlen(pToFill);
            fwrite(pToFill, len, 1, p);
            fclose(p);
            printf("\nWritten Successfuly in the file.\n");

            // pToFill contains the string
            
            //-------------------------------------------------------
            // clean-up
            //-------------------------------------------------------
            //lete \[\] pWideCharBuf ;
            
            // don't forget to
            //delete \[\] pToFill ;
            return 0;
            

            }

            please help

            M Offline
            M Offline
            MicroVirus
            wrote on last edited by
            #5

            You should be aware that all solutions involving wcstombs and related depend on the locale and the typical encoding might not be what you call 'ascii'. Actually, what is called ASCII does not contain any Arabic characters whatsoever. Probably, you are referring to a specific codepage that uses ASCII + some Arabic characters? In that case, it's probably a good idea to set the proper locale before conversion (see Richard MacCutchans post for info), so you get the correct codepage and not, as you probably get with the code given so far, UTF-8 encoding, which means that the text file will contain multiple bytes per character. For instance, see http://stackoverflow.com/questions/2190190/wcstombs-character-encoding[^]. Probably something like setlocale( LC_ALL, "ar.1256" ); will do what you need; it should set the codepage to windows 1256 (which is used for arabic) and use the (generic) Arabic locale. Locales and encoding are a quite complicated issue, so you should be careful and precise when using them. Also, by C++ definition the locale employ system-dependent strings for locale, so if should cater to the platform you are coding for.

            K 1 Reply Last reply
            0
            • L Lost User

              Try the following:

              \_locale\_t locale =  \_create\_locale(LC\_CTYPE, "Arabic");
              errno\_t ret = \_wcstombs\_s\_l(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen, locale);
              

              Use the best guess

              K Offline
              K Offline
              khushboo gupta
              wrote on last edited by
              #6

              Thank you so much sir It solved my problem.

              1 Reply Last reply
              0
              • M MicroVirus

                You should be aware that all solutions involving wcstombs and related depend on the locale and the typical encoding might not be what you call 'ascii'. Actually, what is called ASCII does not contain any Arabic characters whatsoever. Probably, you are referring to a specific codepage that uses ASCII + some Arabic characters? In that case, it's probably a good idea to set the proper locale before conversion (see Richard MacCutchans post for info), so you get the correct codepage and not, as you probably get with the code given so far, UTF-8 encoding, which means that the text file will contain multiple bytes per character. For instance, see http://stackoverflow.com/questions/2190190/wcstombs-character-encoding[^]. Probably something like setlocale( LC_ALL, "ar.1256" ); will do what you need; it should set the codepage to windows 1256 (which is used for arabic) and use the (generic) Arabic locale. Locales and encoding are a quite complicated issue, so you should be careful and precise when using them. Also, by C++ definition the locale employ system-dependent strings for locale, so if should cater to the platform you are coding for.

                K Offline
                K Offline
                khushboo gupta
                wrote on last edited by
                #7

                Thanks Sir for your reply. It solved my problem.

                1 Reply Last reply
                0
                • L Lost User

                  Try the following:

                  \_locale\_t locale =  \_create\_locale(LC\_CTYPE, "Arabic");
                  errno\_t ret = \_wcstombs\_s\_l(&convertedChars, pToFill, maxCharLen, pWideCharBuf, maxCharLen, locale);
                  

                  Use the best guess

                  K Offline
                  K Offline
                  khushboo gupta
                  wrote on last edited by
                  #8

                  Hello Sir, The solution which you have given is working fine for me in windows machine But now I need to implement the same thing (utf-8 to ascii conversion) in a POS terminal which neither has 'locale.h' nor _wcstombs_s_l in the stdlib. The POS terminal has linux kernal.My quries are : 1)Instead of using the function _wcstombs_s_l and create_locale can I replace it with the function definition itself. 2)If I can then please if possible provide me the link where I can find the proper definition of these two functions. 3)If i can not then is there any other way to implement utf8 to ascii conversion. Please help. Thanks

                  L 1 Reply Last reply
                  0
                  • K khushboo gupta

                    Hello Sir, The solution which you have given is working fine for me in windows machine But now I need to implement the same thing (utf-8 to ascii conversion) in a POS terminal which neither has 'locale.h' nor _wcstombs_s_l in the stdlib. The POS terminal has linux kernal.My quries are : 1)Instead of using the function _wcstombs_s_l and create_locale can I replace it with the function definition itself. 2)If I can then please if possible provide me the link where I can find the proper definition of these two functions. 3)If i can not then is there any other way to implement utf8 to ascii conversion. Please help. Thanks

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    You need to research some of these links[^].

                    Use the best guess

                    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