How to convert Ansi string to wide string

Posted on 2001-08-06
Last Modified: 2007-12-19
I'm looking for a method to convert an ANSI string to a wide string.
Question by:CornDog932
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 6354924

for ansi to wide chars only : scan the string an cast each element, in code :

char *in;
short *out;
// scan the entire string
while (*in != 0)
   // convert each char
   *out = (short)*in;
// add terminator zero
*out = 0;

Note: add some code to keep ref to beginning of strings, allocate memory, etc. Other variants based on strlen can be done easily.

On Windows platforms there is also a standard called "multi byte chars", they are regular ANSI (1byte) for regular chars and wide (two bite) for special chars. Windows provides  functions to convert between multibyte and wide (unicode). The function "MultiByteToWideChar" can also be used to convert pure ansi strings. (reverse function : "WideCharToMultiByte")

Hope this helps,

Piet V.
LVL 30

Accepted Solution

Axter earned 100 total points
ID: 6354950
If you're converting std::string to std::wstring or std::wstring to std::string, the following function will convert it.

template<typename SRC, typename DEST>
DEST ConvertText(DEST &dest, const SRC &src)
{//Use this to copy (wstring->string) or (string->wstring)
     dest.resize(src.size(),' ');
     return dest;

std::wstring testwidestr = L"Hello World";
std::string tmpstr = "";
     std::cout << ConvertText(tmpstr,testwidestr) << std::endl;
     tmpstr = "My Name Is Axter";
     std::string tmpstr2 = "";

Expert Comment

ID: 6354973

Nice template version of my code snippit. Beware, Like my snippit, it does not take into account the multi byte characters (special chars).


Piet V;

Note1: the UNICODE end of line char on windows (CR - LF pair) is coded int a single 16bit value instead of two 16bit values as you would expect from converting the chars to shorts directly. The windows conversion functions do the trick.

Note2: To verify a converted text file I normally use NotePad on winNT (it shows all anomalies, where word pad is more tolerant to end of line faults.)
LVL 30

Expert Comment

ID: 6354982
>>Nice template version of my code snippit.
IMHO my code has little reseblence to your code.
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Author Comment

ID: 6355007
Thanks for the extra information, but I'm programming in UNIX.

Author Comment

ID: 6355013
Thanks Axter.
I just tried it out and it seems to work.

Thanks for your post.  I went with Axter's method because it looked like a safer method.
Besides that, I need to get myself use to using templates.

Expert Comment

ID: 6355041
your welcome,
Templates are definitely the way to go. You might want to have a look at the code of the templates you use in this situation (or step trough it wit the debugger). Once you get into it, templates are not that difficult to read. From my experience, the first time is the most difficult step.

In this case the .begin() and .end() methods are equivalent to the memory pointers in the straigtforward code. The copy algorithm is equivalent to the while loop.
LVL 22

Expert Comment

ID: 6355362
What wide character set are you using?  That will work for UNICODE but might not work for others.   It assumes that the ASCII characters covert to the same values in the wide character set.  That is not necessarily true.
LVL 22

Expert Comment

ID: 6355363
What wide character set are you using?  That will work for UNICODE but might not work for others.   It assumes that the ASCII characters covert to the same values in the wide character set.  That is not necessarily true.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now