Converting wchar_t* to char*
Hello fellas,
I have encountered a problem during the converting process.
Here below is my function to convert wchar_t * to char*
void wtoc(char* Dest,const wchar_t *Source)
{
int i = 0;
while(Source[i] != '\0')
{
Dest[i] = (char)Source[i];
++i;
}
}
it compiles succesfully however on linking phase, it gives out an error
undefined reference to `mynamespace::myfunc::wtoc(char*, wchar_t const*)'
and it is really confusing because I don't have a function with a prototype like that. my prototype is
void wtoc(char* Dest,const wchar_t *Source)
so what is wrong with me in here? And also can you suggest any alternative ways to convert wchar_t to char
Thanx in advance.
DK
Re: Converting wchar_t* to char*
My guess is that the function you posted is below where you call it. Try declaring it at the top of your program, if that doesn't work, show us the whole program.
Re: Converting wchar_t* to char*
Have you tried something like the following?
Code:
void mynamespace::wtoc(char* Dest,const wchar_t *Source)
{
int i = 0;
while(Source[i] != '\0')
{
Dest[i] = (char)Source[i];
++i;
}
}
Re: Converting wchar_t* to char*
Quote:
Originally Posted by
vurgac
Code:
void wtoc(char* Dest,const wchar_t *Source)
{
int i = 0;
while(Source[i] != '\0')
{
Dest[i] = (char)Source[i];
++i;
}
}
As Zaccheus suggested, you have probably declared your function in the [nested] namespace, but defined it as a global.
Off-top:
1. Your destination string is NOT zero-terminated
2. I don't think this is how you convert wide char to char
Re: Converting wchar_t* to char*
Quote:
Originally Posted by
VladimirF
2. I don't think this is how you convert wide char to char
The precise conversion is dependent on the source and destination encoding, of course, which is distinct from but related to the char size. So long as only (7-bit) ASCII text is present and the wide characters use UCS-2 or UTF-16, simply casting to char should work. A full conversion from UTF-16 to UTF-8 would require more complex logic, of course.
Re: Converting wchar_t* to char*
Quote:
Originally Posted by
Lindley
The precise conversion is dependent on the source and destination encoding, of course, which is distinct from but related to the char size. So long as only (7-bit) ASCII text is present and the wide characters use UCS-2 or UTF-16, simply casting to char should work. A full conversion from UTF-16 to UTF-8 would require more complex logic, of course.
This statement is similar to:
Code:
void longtoc(char* Dest,const long Source)
{
*Dest = (char)Source;
}
function will convert long value to char, provided that only 7-bit values were used :)
Re: Converting wchar_t* to char*
Re: Converting wchar_t* to char*
To properly convert wchar_t to char you may use wcsrtombs() standard function. You may specify any encoding to use and the function is completely safe.