utf to ascii conversion
-
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_WARNINGSint 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
-
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_WARNINGSint 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
-
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
-
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
-
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_WARNINGSint 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
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. -
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
Thank you so much sir It solved my problem.
-
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.Thanks Sir for your reply. It solved my problem.
-
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
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
-
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