Go Premium for a chance to win a PS4. Enter to Win

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

char ptr to wstring - Urgent!

Hi all,

the following code doesn't worK:

      wchar_t *WcString = new wchar_t[20];
      
      size_t Number = sizeof(m_pcRegName);
      size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
      wstring wsFilePath = WcString;

It will compile, but WcString won't have the desired value.  m_pcRegName points to "C:\", which is translated into "???????..." by the mbstowcs function. What am I doing wrong? Is there any easy way of doing this conversion?

cheers,

henrik
0
henrikatwork
Asked:
henrikatwork
  • 5
  • 4
  • 3
1 Solution
 
jkrCommented:
I'd make it read

    wchar_t *WcString = new wchar_t[strlen(m_pcRegName) + 1];
   
     size_t Number = strlen(m_pcRegName); // <---------------------!!!!!
    size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
    wstring wsFilePath = WcString;

because 'sizeof(char*)' == 4;
0
 
henrikatworkAuthor Commented:
Thanks,

it works much better, BUT my WcString has a bunch of question marks in it!! (That are not in the m_pcRegName)

How come??

cheers,

henrik
0
 
jkrCommented:
How are you trying to display/use the wide string?

wcout << wsFilePath.c_str();

should yield a correct result.
0
Independent Software Vendors: 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!

 
henrikatworkAuthor Commented:
I'm doing an append:


      wsFileName.append( wsFilePath );

wsFileName.append( "myFile.file" );

which turns out to "C:\???myFile.file"
:-(((

0
 
henrikatworkAuthor Commented:
I've tried using C: (without the backslash) , but then I get 4 '?' (????) instead of 3
0
 
rstaveleyCommented:
mbstowcs assumes that m_pcRegName points to a multi-byte string.

Try:
--------8<--------
#include <iostream>
#include <string>

int main()
{
      const char m_pcRegName[] = "Hello, World";
      std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
      std::wcout << wstr << L'\n';
}
--------8<--------

This assumes that m_pcRegName points to an ordinary narrow character string.

Worth a try, perhaps.
0
 
rstaveleyCommented:
Should you be doing...

  wsFileName.append(L"myFile.file");
0
 
jkrCommented:
Hmm, try

   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );

   wcout << WcString << endl;

   wstring wsFilePath = WcString;

   wcout << wsFilePath.c_str();

If it still does not work, give


   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   wsprintfW ( WcString, "%S", m_pcRegName);

a try.

0
 
henrikatworkAuthor Commented:
Sorry,

but please post stuff that compile.

cheers,

henrik


0
 
jkrCommented:
>>but please post stuff that compile.

???
0
 
henrikatworkAuthor Commented:
sorry, that comment was for rstavely, after having posted the code

nt main()
{
     const char m_pcRegName[] = "Hello, World";
     std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
     std::wcout << wstr << L'\n';
}
--------8<--------

which isn't even close to correct...

Thanks for your help jkr!!
henrik
0
 
rstaveleyCommented:
The excerpt compiles and executes on GCC3.2 and VC7.1, if you cut and copy between the indicated scissor marks, but I'm glad jkr was able to get you sorted out OK. My line of thought was that m_pcRegName might really not have been a multi-byte string (the wstr constructor is an iterator constructor), but clearly I was wrong, wrong, wrong...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now