[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1568
  • Last Modified:

How to convert Ansi string to wide string

I'm looking for a method to convert an ANSI string to a wide string.
0
CornDog932
Asked:
CornDog932
  • 3
  • 2
  • 2
  • +1
1 Solution
 
pverhoevCommented:
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
 
AxterCommented:
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
 
pverhoevCommented:
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
Industry Leaders: 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!

 
AxterCommented:
>>Nice template version of my code snippit.
IMHO my code has little reseblence to your code.
0
 
CornDog932Author Commented:
pverhoev,
Thanks for the extra information, but I'm programming in UNIX.
0
 
CornDog932Author Commented:
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
 
pverhoevCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now