0

I'm trying to send a "string" from a C++ client to a Java server. On the server-side the username is a String. In my IDL it's a WStringValue, so I have to send the username as a WStringValue from my client. The following code works fine on both sides:

const CORBA::WChar* usern = (wchar_t*)L"Chuck Norris";
CORBA::WStringValue* username = new CORBA::WStringValue(usern); 

But I want to get the user name from the keyboard... Now, my question is how to convert the char[] to a Wchar*? After my experiments, it seems there is a problem with encoding too..

cout << "Please type your Username: " << endl;
fgets(input, MAX_LINE, stdin);
strcpy(username, input);

const CORBA::WChar* usern = (wchar_t*)username;
CORBA::WStringValue* username = new CORBA::WStringValue(usern);

How can I do this?

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Simon
  • 197
  • 1
  • 2
  • 7

1 Answers1

2

You may try to do it the following way:

std::string str (input);
std::wstring ws;
ws.assign (str.begin(), str.end());
const CORBA::WChar* usern = ws.c_str (); // warning: it will live only while ws lives
Predelnik
  • 5,066
  • 2
  • 24
  • 36
  • I'am such a noob! How do I convert it vice-versa..?! :( From WStringValue* to std::string? Sorry, for that question but I would really appreciate it, if you can help me again...! – Simon Feb 09 '14 at 12:27
  • @Simon well, try to do it backwards: `wstring ws (WStringValue); string s; s.assign (ws.begin(), ws.end());` – Predelnik Feb 09 '14 at 14:27