[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to convert Ansi string to wide string

Posted on 2001-08-06
9
Medium Priority
?
1,545 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 400 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
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!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

649 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