Solved

How to convert Ansi string to wide string

Posted on 2001-08-06
9
1,508 Views
Last Modified: 2007-12-19
I'm looking for a method to convert an ANSI string to a wide string.
0
Comment
Question by:CornDog932
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 

Expert Comment

by:pverhoev
ID: 6354924
Hi,

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;
   out++;
   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.
0
 
LVL 30

Accepted Solution

by:
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(),' ');
     std::copy(src.begin(),src.end(),dest.begin());
     return dest;
}

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

Expert Comment

by:pverhoev
ID: 6354973
Hi,

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

yours,

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.)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 6354982
>>Nice template version of my code snippit.
IMHO my code has little reseblence to your code.
0
 

Author Comment

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

Author Comment

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

pverhoev,
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.
0
 

Expert Comment

by:pverhoev
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.
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 22

Expert Comment

by:nietod
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

718 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