-
March 24th, 2010, 03:20 AM
#1
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
-
March 24th, 2010, 07:21 AM
#2
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.
-
March 25th, 2010, 12:09 PM
#3
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;
}
}
-
March 25th, 2010, 12:58 PM
#4
Re: Converting wchar_t* to char*
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
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 25th, 2010, 03:38 PM
#5
Re: Converting wchar_t* to char*
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.
-
March 26th, 2010, 01:33 PM
#6
Re: Converting wchar_t* to char*
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
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 26th, 2010, 03:02 PM
#7
Re: Converting wchar_t* to char*
-
March 27th, 2010, 01:15 PM
#8
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|