Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing ANSI(char, zstring, csrting, LPSTR) to UNICODE(BSTR, VB string, wchar_t*,LPWSTR) and viceversa

wchar_t* to char*

changing ANSI(char, zstring, csrting, LPSTR) to UNICODE(BSTR, VB string, wchar_t*,LPWSTR) and viceversa

by  Cagliostro  Posted    (Edited  )
Hi, there is a short sample on using, BSTR in combination with char*.
1. There are some basics. ANSI strings have single byte for a character. Because of that, in a string cannot be many than 256 different symbols. I do not mean there the length of string. You can find char*, unsigned char*, const char*, LPSTR, LPTSTR, LPCSTR (if not defined Unicode). They are quite the same thing. If compiler requires one of them and you're using other one, just do a type cast. All these strings in C are null terminated. ThatÆs because, they can't contain character æ\0Æ inside the string (they can contain every characters, but many ANSI functions will ignore all string after that character). Old basic string contained in the first character the string length. Large basic string contained the length in the first two characters, and the string was single byte for characters.
UNICODE is double byte for a character. As result, UNICODE character can be one of 65000 symbols. It quite is enough to code any characters for all existing languages in the world.
See BSTR, unsigned short*, wchar_t*, BSTR, LPWSTR, LPCTSTR (if defined UNICODE) are synonyms. They are NULL terminated in C/C++. In VB they contain length in first two bytes.
When you declare an ANSI string you do like
char* xx = "hello";
When you declare an UNICODE string you do like
wchar_t* xx = L"hello";//pay attention on L

The best way of using them in my opinion is _bsrt_t from ATL. If you want to use char, use type cast to char*, if wchar_t* the type cast to wchar_t*. See using of _bstr_t:


Code:
#include<comutil.h>//for _bstr_t
#include<iostream>
using namespace std;//for iostream
int main()
{
  BSTR bx = L"Hello ";
  char* cx = "world"
  _bstr_t x = bx;
  bx += cx;
  wcout<< bx;
  cout<< cx<< endl;
  wcout<< (BSTR)x<< endl;
  cout<< (char*)x<< endl;
  BSTR by = (BSTR)x;
  char cy = (char*)x;
  wcout<< by<< endl;
  cout<< cy<< endl;
  cout<< "there was shown some tehnics on using strings in ATL :)"<< endl;
  return 0;
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top