473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing CString with std::string and std::wstring

bajajv
152 New Member
Hi, I was trying to implement CString with std::string and std::wstring.
class CString
{
public:
std::string str;
CString() {str = "ABCD";} //this works
}:

class CString
{
public:
std::wstring wstr;
CString() {wstr = "ABCD";} //this doesn't works
}:
The wstring is giving error for =operator. What do I need to add here for wstring?
I am using MS visual studio 2005, but I want to make it compiler independent.
Dec 8 '09 #1
10 8273
Banfa
9,065 Recognized Expert Moderator Expert
What error is it giving?

Have you tried L"ABCD"?
Dec 8 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your Visual Studio project has a charactert set property. This is where you specify char or Unicode. In your code there should never be explicit char or wide char syntax.

You write your code to use the Microsoft-specific amppings between char and Unicode. Do reasearch on the TCHAR mappings.

Note that Windoes does all processing using Unicode. Unless your program specifically needs the char character set, there should be no use of char in your program.
Dec 8 '09 #3
bajajv
152 New Member
I want to write a CString which can be OS independent. Is this possible with wide char? I am trying for UNICODE only.
Dec 10 '09 #4
bajajv
152 New Member
Thanks Banfa. I missed that.
Dec 10 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
I think you may have a little trouble creating an OS independent multi-byte/wide character string because I believe Windows uses wide characters, that is characters with 16 or more bits per character like UNICODE where as Linux uses multi-byte characters that is the bytes are all 8 bits but sometimes a characters uses more than 1 of them like UTF8.

With wide characters all the characters are the same width (16 bits I think for UNICODE), with multi-byte characters not all characters are the same width, in UTF8 characters can be 1,2,3 or 4 bytes. The first few bits of the byte tell you whether there are more bytes to follow. Additionally in UTF8 there is a 1 - 1 mapping of the first 127 ASCII codes and the first 127 UTF8 codes.

Anyway in Linux a std::string can be used to hold either an ASCII string or a UTF8 string since the basic data unit of both is 8 bit octets. This is not true for Windows if the program is compiled to use UNICODE then it needs to use wide characters wchar_t (although as weaknessforcats says you shouldn't use them directly but through the TCHAR type) which actually have more bits per character (like I siad I think 16).

Because of this difference I think you would find it hard to make a OS independent wide/multi-byte character type.
Dec 10 '09 #6
bajajv
152 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <stdarg.h>
  4.  
  5. using namespace std;
  6.  
  7. class MyString
  8. {
  9. public:
  10.    wstring wStr;
  11.    MyString ()  {wStr = L"";}
  12.    MyString (wstring str)  {wStr = str;}
  13.    void Format(const wchar_t*lpszFormat, ...);   
  14. };
  15.  
  16. void MyString::Format(const wchar_t* lpszFormat, ...)
  17. {
  18.     wchar_t szBuffer[256];
  19.     va_list args;
  20.     va_start(args, lpszFormat);
  21.     vswprintf(szBuffer, 256, lpszFormat, args);
  22.     va_end (args);
  23.     // Got the string in szBuffer, Now convert to a wchar_t*
  24.     wcscpy(const_cast<wchar_t*>(this->wStr.c_str()), szBuffer);
  25. }
  26.  
  27. int main()
  28. {
  29.     wstring firstName = L"Vipul";
  30.     wstring lastName = L"Bajaj";
  31.     MyString myStr (firstName); 
  32.     wprintf(L"%s\n", myStr.wStr.c_str());
  33.     cout<<myStr.wStr.c_str()<<endl;
  34.     myStr.Format(L"%s%s", firstName.c_str(), lastName.c_str());
  35.     wprintf(L"%s\n", myStr.wStr.c_str());
  36.  
  37. return 0;
  38. }
The above code works fine, but doesnt works when I change the format control string.

e.g. if I change the format string to "%s %s", the code below will not work -
wcscpy(const_ca st<wchar_t*>(th is->wStr.c_str() ), szBuffer);
How can I improve this?
is there any way to accomodate that space, or a slash '\' or anything in between those two '%s'?
Dec 12 '09 #7
bajajv
152 New Member
One reason could be that the space between - "%s %s" - is in ASCII... but it should also be wide character, as I am providing it using an L - L"%s %s"... Please clear this doubt.. Is this L converting the space also to wide character?
Dec 12 '09 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
Why aren't you using:

Expand|Select|Wrap|Line Numbers
  1. wcout << myStr << endl;
?

Avoid the printf family of functions. They are from C and they do not allow print of user defined types so there's no way to print a Date or a Person object.

A wcscpy does not convert a char string to a wchar_t string. You need to call MultiByteToWide String.

Again, all of this is done for you already if you use TCHAR.
Dec 12 '09 #9
bajajv
152 New Member
But I cant use windows specific things. I need to make it work on both windows and unix.
Dec 14 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
12287
by: red floyd | last post by:
I have a an app that I'm writing which uses char and std::string. I'm using a library which expects wchar_t arrays. Is there a standard way to convert between std::string and std::wstring, or do I need to use something like std::transform()? Thanks
12
28199
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
3
5510
by: Lars Nielsen | last post by:
Hey there I have a win32 application written i c++. I have a std::vector of std::string's i will fill with filenames. typedef vector<std::string> strvector; strvector vFiles; WIN32_FIND_DATA fd;
9
22564
by: vsgdp | last post by:
Hi, Is there a unicode equivalent to std::string?
5
48700
by: Karthik | last post by:
Hello, How can I convert a BSTR data type to std::string??? Thanks a much! Karthik
37
3812
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system is quite importante - it's not a realtime system, but almost - and I concern about std::string performance in terms of speed. No doubt to use std implementation is a lot easier but I can't sacrifice speed.
14
12195
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++ books (Effective C++, More Effective C++, Exceptional C++, More Exceptional C++, C++ FAQs, Addison Wesley 2nd Edition) Authors of these books have not considered UNICODE. So many of their
10
10137
by: Jeffrey Walton | last post by:
Hi All, I've done a little homework (I've read responses to similar from P.J. Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below is what I am performing (Stroustrup's Appendix D recommendation won't compile in Microsoft VC++ 6.0). My question is in reference to MultiByte Character Sets. Will this code perform as expected? I understand every problem has a simple and elegant solution that is wrong.
3
3703
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.